func handle(conn net.Conn, method uint8) error { defer conn.Close() req, err := gosocks5.ReadRequest(conn) if err != nil { log.Println(err) return err } tconn, err := Connect(req.Addr.String()) if err != nil { log.Println(err) return err } defer tconn.Close() rep := gosocks5.NewReply(gosocks5.Succeeded, nil) if err := rep.Write(conn); err != nil { return err } if err := Transport(conn, tconn); err != nil { log.Println(err) } return nil }
func handleSocks5(conn net.Conn, methods []uint8) { if err := selectMethod(conn, methods...); err != nil { log.Println(err) return } req, err := gosocks5.ReadRequest(conn) if err != nil { return } //log.Println(req) sconn, err := makeTunnel() if err != nil { gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) log.Println(err) return } defer sconn.Close() switch req.Cmd { case gosocks5.CmdConnect, gosocks5.CmdBind: if err := req.Write(sconn); err != nil { return } Transport(conn, sconn) case gosocks5.CmdUdp: if err := req.Write(sconn); err != nil { return } rep, err := gosocks5.ReadReply(sconn) if err != nil || rep.Rep != gosocks5.Succeeded { return } uconn, err := net.ListenUDP("udp", nil) if err != nil { log.Println(err) gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return } defer uconn.Close() addr := ToSocksAddr(uconn.LocalAddr()) addr.Host, _, _ = net.SplitHostPort(conn.LocalAddr().String()) log.Println("udp:", addr) rep = gosocks5.NewReply(gosocks5.Succeeded, addr) if err := rep.Write(conn); err != nil { log.Println(err) return } go cliTunnelUDP(uconn, sconn) // block, waiting for client exit ioutil.ReadAll(conn) } }
func socks5Handle(conn net.Conn) { defer conn.Close() req, err := gosocks5.ReadRequest(conn) if err != nil { log.Println(err) return } switch req.Cmd { case gosocks5.CmdConnect: //log.Println("connect", req.Addr.String()) tconn, err := Connect(req.Addr.String(), Proxy) if err != nil { gosocks5.NewReply(gosocks5.HostUnreachable, nil).Write(conn) return } defer tconn.Close() rep := gosocks5.NewReply(gosocks5.Succeeded, nil) if err := rep.Write(conn); err != nil { return } if err := Transport(conn, tconn); err != nil { //log.Println(err) } case gosocks5.CmdBind: l, err := net.ListenTCP("tcp", nil) if err != nil { gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return } addr := ToSocksAddr(l.Addr()) addr.Host, _, _ = net.SplitHostPort(conn.LocalAddr().String()) log.Println("bind:", addr) rep := gosocks5.NewReply(gosocks5.Succeeded, addr) if err := rep.Write(conn); err != nil { return } tconn, err := l.AcceptTCP() if err != nil { log.Println("accept:", err) gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return } defer tconn.Close() l.Close() addr = ToSocksAddr(tconn.RemoteAddr()) log.Println("accept peer:", addr.String()) rep = gosocks5.NewReply(gosocks5.Succeeded, addr) if err := rep.Write(conn); err != nil { log.Println(err) return } if err := Transport(conn, tconn); err != nil { //log.Println(err) } case gosocks5.CmdUdp: uconn, err := net.ListenUDP("udp", nil) if err != nil { log.Println(err) gosocks5.NewReply(gosocks5.Failure, nil).Write(conn) return } defer uconn.Close() addr := ToSocksAddr(uconn.LocalAddr()) addr.Host, _, _ = net.SplitHostPort(conn.LocalAddr().String()) log.Println("udp:", addr) rep := gosocks5.NewReply(gosocks5.Succeeded, addr) if err := rep.Write(conn); err != nil { log.Println(err) return } srvTunnelUDP(conn, uconn) } }
func handleConn(conn net.Conn, arg Args) { atomic.AddInt32(&connCounter, 1) if glog.V(LINFO) { glog.Infof("%s connected, connections: %d", conn.RemoteAddr(), atomic.LoadInt32(&connCounter)) } if glog.V(LINFO) { defer func() { glog.Infof("%s disconnected, connections: %d", conn.RemoteAddr(), atomic.LoadInt32(&connCounter)) }() } defer atomic.AddInt32(&connCounter, -1) defer conn.Close() selector := &serverSelector{ methods: []uint8{ gosocks5.MethodNoAuth, gosocks5.MethodUserPass, MethodTLS, MethodTLSAuth, }, arg: arg, } switch arg.Protocol { case "ss": // shadowsocks return case "http": req, err := http.ReadRequest(bufio.NewReader(conn)) if err != nil { if glog.V(LWARNING) { glog.Warningln("http:", err) } return } handleHttpRequest(req, conn, arg) return case "socks", "socks5": conn = gosocks5.ServerConn(conn, selector) req, err := gosocks5.ReadRequest(conn) if err != nil { if glog.V(LWARNING) { glog.Warningln("socks5:", err) } return } handleSocks5Request(req, conn, arg) return } // http + socks5 b := make([]byte, 16*1024) n, err := io.ReadAtLeast(conn, b, 2) if err != nil { if glog.V(LWARNING) { glog.Warningln("client:", err) } return } if b[0] == gosocks5.Ver5 { mn := int(b[1]) // methods count length := 2 + mn if n < length { if _, err := io.ReadFull(conn, b[n:length]); err != nil { if glog.V(LWARNING) { glog.Warningln("socks5:", err) } return } } methods := b[2 : 2+mn] method := selector.Select(methods...) if _, err := conn.Write([]byte{gosocks5.Ver5, method}); err != nil { if glog.V(LWARNING) { glog.Warningln("socks5:", err) } return } c, err := selector.OnSelected(method, conn) if err != nil { if glog.V(LWARNING) { glog.Warningln("socks5:", err) } return } conn = c req, err := gosocks5.ReadRequest(conn) if err != nil { if glog.V(LWARNING) { glog.Warningln("socks5:", err) } return } handleSocks5Request(req, conn, arg) return } req, err := http.ReadRequest(bufio.NewReader(newReqReader(b[:n], conn))) if err != nil { if glog.V(LWARNING) { glog.Warningln("http:", err) } return } handleHttpRequest(req, conn, arg) }
func (s *ProxyServer) handleConn(conn net.Conn) { defer conn.Close() switch s.Node.Protocol { case "ss": // shadowsocks server := NewShadowServer(ss.NewConn(conn, s.cipher.Copy()), s) server.OTA = s.Node.getBool("ota") server.Serve() return case "http": req, err := http.ReadRequest(bufio.NewReader(conn)) if err != nil { glog.V(LWARNING).Infoln("[http]", err) return } NewHttpServer(conn, s).HandleRequest(req) return case "socks", "socks5": conn = gosocks5.ServerConn(conn, s.selector) req, err := gosocks5.ReadRequest(conn) if err != nil { glog.V(LWARNING).Infoln("[socks5]", err) return } NewSocks5Server(conn, s).HandleRequest(req) return } // http or socks5 b := make([]byte, MediumBufferSize) n, err := io.ReadAtLeast(conn, b, 2) if err != nil { glog.V(LWARNING).Infoln(err) return } // TODO: use bufio.Reader if b[0] == gosocks5.Ver5 { mn := int(b[1]) // methods count length := 2 + mn if n < length { if _, err := io.ReadFull(conn, b[n:length]); err != nil { glog.V(LWARNING).Infoln("[socks5]", err) return } } // TODO: use gosocks5.ServerConn methods := b[2 : 2+mn] method := s.selector.Select(methods...) if _, err := conn.Write([]byte{gosocks5.Ver5, method}); err != nil { glog.V(LWARNING).Infoln("[socks5] select:", err) return } c, err := s.selector.OnSelected(method, conn) if err != nil { glog.V(LWARNING).Infoln("[socks5] onselected:", err) return } conn = c req, err := gosocks5.ReadRequest(conn) if err != nil { glog.V(LWARNING).Infoln("[socks5] request:", err) return } NewSocks5Server(conn, s).HandleRequest(req) return } req, err := http.ReadRequest(bufio.NewReader(&reqReader{b: b[:n], r: conn})) if err != nil { glog.V(LWARNING).Infoln("[http]", err) return } NewHttpServer(conn, s).HandleRequest(req) }