Esempio n. 1
0
func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
	log.Info("Freedom: Opening connection to ", firstPacket.Destination())

	var conn net.Conn
	err := retry.Timed(5, 100).On(func() error {
		rawConn, err := dialer.Dial(firstPacket.Destination())
		if err != nil {
			return err
		}
		conn = rawConn
		return nil
	})
	if err != nil {
		close(ray.OutboundOutput())
		log.Error("Freedom: Failed to open connection to ", firstPacket.Destination(), ": ", err)
		return err
	}
	defer conn.Close()

	input := ray.OutboundInput()
	output := ray.OutboundOutput()
	var readMutex, writeMutex sync.Mutex
	readMutex.Lock()
	writeMutex.Lock()

	if chunk := firstPacket.Chunk(); chunk != nil {
		conn.Write(chunk.Value)
		chunk.Release()
	}

	if !firstPacket.MoreChunks() {
		writeMutex.Unlock()
	} else {
		go func() {
			v2io.ChanToRawWriter(conn, input)
			writeMutex.Unlock()
		}()
	}

	go func() {
		defer readMutex.Unlock()
		defer close(output)

		var reader io.Reader = conn

		if firstPacket.Destination().IsUDP() {
			reader = v2net.NewTimeOutReader(16 /* seconds */, conn)
		}

		v2io.RawReaderToChan(output, reader)
	}()

	writeMutex.Lock()
	if tcpConn, ok := conn.(*net.TCPConn); ok {
		tcpConn.CloseWrite()
	}
	readMutex.Lock()

	return nil
}
func (this *InboundDetourHandlerDynamic) refresh() error {
	this.lastRefresh = time.Now()

	for _, ich := range this.ich2Recycle {
		port2Delete := ich.Port()

		ich.Close()
		err := retry.Timed(100 /* times */, 1000 /* ms */).On(func() error {
			port := this.pickUnusedPort()
			err := ich.Listen(port)
			if err != nil {
				log.Error("Point: Failed to start inbound detour on port ", port, ": ", err)
				return err
			}
			this.portsInUse[port] = true
			return nil
		})
		if err != nil {
			continue
		}

		delete(this.portsInUse, port2Delete)
	}

	this.Lock()
	this.ich2Recycle, this.ichInUse = this.ichInUse, this.ich2Recycle
	this.Unlock()

	return nil
}
Esempio n. 3
0
// 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("Invalid port ", this.port)
		return BadConfiguration
	}

	err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
		err := this.ich.Listen(this.port)
		if err != nil {
			return err
		}
		log.Warning("Point server 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
}
Esempio n. 4
0
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
}
Esempio n. 5
0
func (this *InboundDetourHandler) Start() error {
	for _, ich := range this.ich {
		return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			err := ich.handler.Listen(ich.port)
			if err != nil {
				return err
			}
			return nil
		})
	}
	return nil
}
Esempio n. 6
0
func (this *DokodemoDoor) AcceptTCPConnections(tcpListener *net.TCPListener) {
	for this.accepting {
		retry.Timed(100, 100).On(func() error {
			connection, err := tcpListener.AcceptTCP()
			if err != nil {
				log.Error("Dokodemo failed to accept new connections: %v", err)
				return err
			}
			go this.HandleTCPConnection(connection)
			return nil
		})
	}
}
Esempio n. 7
0
func (server *SocksServer) AcceptConnections(listener *net.TCPListener) {
	for server.accepting {
		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			connection, err := listener.AcceptTCP()
			if err != nil {
				log.Error("Socks failed to accept new connection %v", err)
				return err
			}
			go server.HandleConnection(connection)
			return nil
		})

	}
}
Esempio n. 8
0
// 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 (vp *Point) Start() error {
	if vp.port <= 0 {
		log.Error("Invalid port %d", vp.port)
		return config.BadConfiguration
	}

	return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
		err := vp.ich.Listen(vp.port)
		if err == nil {
			log.Warning("Point server started on port %d", vp.port)
			return nil
		}
		return err
	})
}
Esempio n. 9
0
func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
	for handler.accepting {
		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			connection, err := listener.AcceptTCP()
			if err != nil {
				log.Error("Failed to accpet connection: %s", err.Error())
				return err
			}
			go handler.HandleConnection(connection)
			return nil
		})

	}
	return nil
}
Esempio n. 10
0
// Starts the inbound connection handler.
func (this *InboundDetourHandler) Start() error {
	for _, ich := range this.ich {
		err := retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			err := ich.handler.Listen(ich.port)
			if err != nil {
				log.Error("Failed to start inbound detour on port %d: %v", ich.port, err)
				return err
			}
			return nil
		})
		if err != nil {
			return err
		}
	}
	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
}
Esempio n. 12
0
func (this *HttpProxyServer) accept() {
	for this.accepting {
		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			this.Lock()
			defer this.Unlock()
			if !this.accepting {
				return nil
			}
			tcpConn, err := this.tcpListener.AcceptTCP()
			if err != nil {
				log.Error("Failed to accept HTTP connection: ", err)
				return err
			}
			go this.handleConnection(tcpConn)
			return nil
		})
	}
}
Esempio n. 13
0
func (this *SocksServer) AcceptConnections() {
	for this.accepting {
		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			this.tcpMutex.RLock()
			defer this.tcpMutex.RUnlock()
			if !this.accepting {
				return nil
			}
			connection, err := this.tcpListener.AcceptTCP()
			if err != nil {
				log.Error("Socks: failed to accept new connection: ", err)
				return err
			}
			go this.HandleConnection(connection)
			return nil
		})
	}
}
Esempio n. 14
0
func (this *DokodemoDoor) AcceptTCPConnections() {
	for this.accepting {
		retry.Timed(100, 100).On(func() error {
			this.tcpMutex.RLock()
			defer this.tcpMutex.RUnlock()
			if !this.accepting {
				return nil
			}
			connection, err := this.tcpListener.AcceptTCP()
			if err != nil {
				log.Error("Dokodemo failed to accept new connections: %v", err)
				return err
			}
			go this.HandleTCPConnection(connection)
			return nil
		})
	}
}
Esempio n. 15
0
func (this *VMessInboundHandler) AcceptConnections() error {
	for this.accepting {
		retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
			this.Lock()
			defer this.Unlock()
			if !this.accepting {
				return nil
			}
			connection, err := this.listener.AcceptTCP()
			if err != nil {
				log.Error("Failed to accpet connection: ", err)
				return err
			}
			go this.HandleConnection(connection)
			return nil
		})

	}
	return nil
}
func (this *InboundDetourHandlerDynamic) refresh() error {
	this.lastRefresh = time.Now()

	config := this.config
	this.ich2Recyle = this.ichs
	newIchs := make([]proxy.InboundHandler, config.Allocation.Concurrency)

	for idx := range newIchs {
		err := retry.Timed(5, 100).On(func() error {
			port := this.pickUnusedPort()
			ich, err := proxyrepo.CreateInboundHandler(config.Protocol, this.space, config.Settings, &proxy.InboundHandlerMeta{
				Address: config.ListenOn, 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
}
Esempio n. 17
0
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().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()
	var readMutex, writeMutex sync.Mutex
	readMutex.Lock()
	writeMutex.Lock()

	conn.Write(payload.Value)

	go func() {
		v2writer := v2io.NewAdaptiveWriter(conn)
		defer v2writer.Release()

		v2io.Pipe(input, v2writer)
		writeMutex.Unlock()
	}()

	go func() {
		defer readMutex.Unlock()

		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)
		defer v2reader.Release()

		v2io.Pipe(v2reader, output)
		ray.OutboundOutput().Close()
	}()

	writeMutex.Lock()
	if tcpConn, ok := conn.(*tcp.RawConnection); ok {
		tcpConn.CloseWrite()
	}
	readMutex.Lock()

	return nil
}
Esempio n. 18
0
func (this *FreedomConnection) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
	log.Info("Freedom: Opening connection to ", firstPacket.Destination())

	var conn net.Conn
	err := retry.Timed(5, 100).On(func() error {
		rawConn, err := dialer.Dial(firstPacket.Destination())
		if err != nil {
			return err
		}
		conn = rawConn
		return nil
	})
	if err != nil {
		close(ray.OutboundOutput())
		log.Error("Freedom: Failed to open connection to ", firstPacket.Destination(), ": ", err)
		return err
	}
	defer conn.Close()

	input := ray.OutboundInput()
	output := ray.OutboundOutput()
	var readMutex, writeMutex sync.Mutex
	readMutex.Lock()
	writeMutex.Lock()

	if chunk := firstPacket.Chunk(); chunk != nil {
		conn.Write(chunk.Value)
		chunk.Release()
	}

	if !firstPacket.MoreChunks() {
		writeMutex.Unlock()
	} else {
		go func() {
			v2net.ChanToWriter(conn, input)
			writeMutex.Unlock()
		}()
	}

	go func() {
		defer readMutex.Unlock()
		defer close(output)

		response, err := v2net.ReadFrom(conn, nil)
		log.Info("Freedom receives ", response.Len(), " bytes from ", conn.RemoteAddr())
		if response.Len() > 0 {
			output <- response
		} else {
			response.Release()
		}
		if err != nil {
			return
		}
		if firstPacket.Destination().IsUDP() {
			return
		}

		v2net.ReaderToChan(output, conn)
	}()

	if this.space.HasDnsCache() {
		if firstPacket.Destination().Address().IsDomain() {
			domain := firstPacket.Destination().Address().Domain()
			addr := conn.RemoteAddr()
			switch typedAddr := addr.(type) {
			case *net.TCPAddr:
				this.space.DnsCache().Add(domain, typedAddr.IP)
			case *net.UDPAddr:
				this.space.DnsCache().Add(domain, typedAddr.IP)
			}
		}
	}

	writeMutex.Lock()
	if tcpConn, ok := conn.(*net.TCPConn); ok {
		tcpConn.CloseWrite()
	}
	readMutex.Lock()

	return nil
}