func startCommunicate(request *vmessio.VMessRequest, dest v2net.Address, ray core.OutboundRay) error { input := ray.OutboundInput() output := ray.OutboundOutput() conn, err := net.DialTCP("tcp", nil, &net.TCPAddr{dest.IP, int(dest.Port), ""}) if err != nil { log.Error("Failed to open tcp (%s): %v", dest.String(), err) close(output) return err } log.Info("VMessOut: Tunneling request for %s", request.Address.String()) defer conn.Close() requestFinish := make(chan bool) responseFinish := make(chan bool) go handleRequest(conn, request, input, requestFinish) go handleResponse(conn, request, output, responseFinish) <-requestFinish conn.CloseWrite() <-responseFinish return nil }
func (vconn *FreedomConnection) Start(ray core.OutboundRay) error { input := ray.OutboundInput() output := ray.OutboundOutput() conn, err := net.Dial("tcp", vconn.dest.String()) if err != nil { close(output) return log.Error("Freedom: Failed to open tcp connection: %s : %v", vconn.dest.String(), err) } log.Info("Freedom: Sending outbound tcp: %s", vconn.dest.String()) readFinish := make(chan bool) writeFinish := make(chan bool) go vconn.DumpInput(conn, input, writeFinish) go vconn.DumpOutput(conn, output, readFinish) go vconn.CloseConn(conn, readFinish, writeFinish) return nil }
func (server *SocksServer) HandleConnection(connection net.Conn) error { defer connection.Close() auth, err := socksio.ReadAuthentication(connection) if err != nil { log.Error("Error on reading authentication: %v", err) return err } expectedAuthMethod := socksio.AuthNotRequired if server.config.AuthMethod == JsonAuthMethodUserPass { expectedAuthMethod = socksio.AuthUserPass } if !auth.HasAuthMethod(expectedAuthMethod) { authResponse := socksio.NewAuthenticationResponse(socksio.AuthNoMatchingMethod) socksio.WriteAuthentication(connection, authResponse) log.Info("Client doesn't support allowed any auth methods.") return ErrorAuthenticationFailed } log.Debug("Auth accepted, responding auth.") authResponse := socksio.NewAuthenticationResponse(socksio.AuthNotRequired) socksio.WriteAuthentication(connection, authResponse) request, err := socksio.ReadRequest(connection) if err != nil { log.Error("Error on reading socks request: %v", err) return err } response := socksio.NewSocks5Response() if request.Command == socksio.CmdBind || request.Command == socksio.CmdUdpAssociate { response := socksio.NewSocks5Response() response.Error = socksio.ErrorCommandNotSupported socksio.WriteResponse(connection, response) log.Info("Unsupported socks command %d", request.Command) return ErrorCommandNotSupported } response.Error = socksio.ErrorSuccess response.Port = request.Port response.AddrType = request.AddrType switch response.AddrType { case socksio.AddrTypeIPv4: copy(response.IPv4[:], request.IPv4[:]) case socksio.AddrTypeIPv6: copy(response.IPv6[:], request.IPv6[:]) case socksio.AddrTypeDomain: response.Domain = request.Domain } log.Debug("Socks response port = %d", response.Port) socksio.WriteResponse(connection, response) ray := server.vPoint.NewInboundConnectionAccepted(request.Destination()) input := ray.InboundInput() output := ray.InboundOutput() finish := make(chan bool, 2) go server.dumpInput(connection, input, finish) go server.dumpOutput(connection, output, finish) server.waitForFinish(finish) return nil }