func tcpServe(localConf *utils.LocalConf) { nl, err := net.Listen("tcp", fmt.Sprintf("%s:%d", localConf.Address, localConf.Port)) if err != nil { utils.Fatalf("Create server error %s", err) } defer nl.Close() utils.Infof("Client listen on %s://%s:%d", localConf.Protocol, localConf.Address, localConf.Port) var handleFunc func(c protocols.Protocol) switch localConf.Protocol { case "http": handleFunc = handleHTTP case "socks5": handleFunc = handleSocks5 } for { c, err := nl.Accept() if err != nil { utils.Errorf("Local connection accept error %s", err) continue } utils.Infof("Get local connection from %s", c.RemoteAddr()) go handleFunc(c) } }
// Serve handles connection between socks5 client and remote addr. func (s *TCPRelay) Serve() (err error) { defer func() { if !s.closed { s.conn.Close() } }() s.handShake() cmd, rawAddr, addr, err := s.parseRequest() if err != nil { utils.Errorf("Parse request error %v\n", err) return } utils.Infof("Proxy connection to %s\n", string(addr)) s.reply() switch cmd { case CONNECT: s.connect(rawAddr) case UDP_ASSOCIATE: s.udpAssociate() case BIND: default: err = fmt.Errorf("unknow cmd type") } return }
func _HTTPHandler(conn protocols.Protocol, req *http.Request) { utils.Infof("Sending request %v %v \n", req.Method, req.URL.Host) rmProxyHeaders(req) dump, err := httputil.DumpRequest(req, true) if err != nil { utils.Fatalf("%s", err) } conn.Write(dump) }
func listen(serverInfo *utils.ServerConf) { nl, err := net.Listen("tcp", fmt.Sprintf("%s:%d", serverInfo.Address, serverInfo.Port)) if err != nil { utils.Fatalf("Create server error %s", err) } utils.Infof("Listen on %d\n", serverInfo.Port) cg := mika.NewCryptoGenerator(serverInfo.Method, serverInfo.Password) for { c, err := nl.Accept() if err != nil { utils.Errorf("Accept connection error %s", err) continue } go func() { tcpConn := &tcp.Conn{c, time.Duration(serverInfo.Timeout) * time.Second} handle(tcpConn, cg) }() } }