// Start starts the Point server, and return any error during the process. // In the case of any errors, the state of the server is unpredicatable. func (this *Point) Start() error { if this.port <= 0 { log.Error("Point: Invalid port ", this.port) return common.ErrBadConfiguration } err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error { err := this.ich.Start() if err != nil { return err } log.Warning("Point: started on port ", this.port) return nil }) if err != nil { return err } for _, detourHandler := range this.idh { err := detourHandler.Start() if err != nil { return err } } return nil }
func (this *FreedomConnection) Dispatch(destination v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error { log.Info("Freedom: Opening connection to ", destination) defer payload.Release() defer ray.OutboundInput().Release() defer ray.OutboundOutput().Close() var conn internet.Connection if this.domainStrategy == DomainStrategyUseIP && destination.Address().Family().IsDomain() { destination = this.ResolveIP(destination) } err := retry.Timed(5, 100).On(func() error { rawConn, err := internet.Dial(this.meta.Address, destination, this.meta.StreamSettings) if err != nil { return err } conn = rawConn return nil }) if err != nil { log.Warning("Freedom: Failed to open connection to ", destination, ": ", err) return err } defer conn.Close() input := ray.OutboundInput() output := ray.OutboundOutput() if !payload.IsEmpty() { conn.Write(payload.Value) } go func() { v2writer := v2io.NewAdaptiveWriter(conn) defer v2writer.Release() v2io.Pipe(input, v2writer) if tcpConn, ok := conn.(*tcp.RawConnection); ok { tcpConn.CloseWrite() } }() var reader io.Reader = conn timeout := this.timeout if destination.IsUDP() { timeout = 16 } if timeout > 0 { reader = v2net.NewTimeOutReader(int(timeout) /* seconds */, conn) } v2reader := v2io.NewAdaptiveReader(reader) v2io.Pipe(v2reader, output) v2reader.Release() ray.OutboundOutput().Close() return nil }
func (this *VMessOutboundHandler) Dispatch(target v2net.Destination, payload *alloc.Buffer, ray ray.OutboundRay) error { defer ray.OutboundInput().Release() defer ray.OutboundOutput().Close() var rec *protocol.ServerSpec var conn internet.Connection err := retry.Timed(5, 100).On(func() error { rec = this.serverPicker.PickServer() rawConn, err := internet.Dial(this.meta.Address, rec.Destination(), this.meta.StreamSettings) if err != nil { return err } conn = rawConn return nil }) if err != nil { log.Error("VMess|Outbound: Failed to find an available destination:", err) return err } log.Info("VMess|Outbound: Tunneling request to ", target, " via ", rec.Destination()) command := protocol.RequestCommandTCP if target.IsUDP() { command = protocol.RequestCommandUDP } request := &protocol.RequestHeader{ Version: encoding.Version, User: rec.PickUser(), Command: command, Address: target.Address(), Port: target.Port(), Option: protocol.RequestOptionChunkStream, } defer conn.Close() conn.SetReusable(true) if conn.Reusable() { // Conn reuse may be disabled on transportation layer request.Option.Set(protocol.RequestOptionConnectionReuse) } input := ray.OutboundInput() output := ray.OutboundOutput() var requestFinish, responseFinish sync.Mutex requestFinish.Lock() responseFinish.Lock() session := encoding.NewClientSession(protocol.DefaultIDHash) go this.handleRequest(session, conn, request, payload, input, &requestFinish) go this.handleResponse(session, conn, request, rec.Destination(), output, &responseFinish) requestFinish.Lock() responseFinish.Lock() return nil }
// Starts the inbound connection handler. func (this *InboundDetourHandlerAlways) Start() error { for _, ich := range this.ich { err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error { err := ich.Start() if err != nil { log.Error("Failed to start inbound detour:", err) return err } return nil }) if err != nil { return err } } return nil }
func GenTestBinaryPath() { testBinaryPathGen.Do(func() { var tempDir string err := retry.Timed(5, 100).On(func() error { dir, err := ioutil.TempDir("", "v2ray") if err != nil { return err } tempDir = dir return nil }) if err != nil { panic(err) } file := filepath.Join(tempDir, "v2ray.test") if runtime.GOOS == "windows" { file += ".exe" } testBinaryPath = file fmt.Printf("Generated binary path: %s\n", file) }) }
func (this *InboundDetourHandlerDynamic) refresh() error { this.lastRefresh = time.Now() config := this.config this.ich2Recyle = this.ichs newIchs := make([]proxy.InboundHandler, config.GetAllocationStrategyValue().Concurrency.GetValue()) for idx := range newIchs { err := retry.Timed(5, 100).On(func() error { port := this.pickUnusedPort() ichConfig, _ := config.GetTypedSettings() ich, err := proxyregistry.CreateInboundHandler(config.Settings.Type, this.space, ichConfig, &proxy.InboundHandlerMeta{ Address: config.GetListenOnValue(), Port: port, Tag: config.Tag, StreamSettings: config.StreamSettings}) if err != nil { delete(this.portsInUse, port) return err } err = ich.Start() if err != nil { delete(this.portsInUse, port) return err } this.portsInUse[port] = true newIchs[idx] = ich return nil }) if err != nil { log.Error("Point: Failed to create inbound connection handler: ", err) return err } } this.Lock() this.ichs = newIchs this.Unlock() return nil }