mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-09-27 22:36:12 -04:00
format code
This commit is contained in:
parent
4046ee968c
commit
9a88e8696a
@ -9,7 +9,7 @@ import (
|
|||||||
type SocksServerFactory struct {
|
type SocksServerFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||||
return NewSocksServer(dispatcher, rawConfig.(*json.SocksConfig)), nil
|
return NewSocksServer(dispatcher, rawConfig.(*json.SocksConfig)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
var udpAddress v2net.Address
|
var udpAddress v2net.Address
|
||||||
|
|
||||||
func (server *SocksServer) ListenUDP(port uint16) error {
|
func (this *SocksServer) ListenUDP(port uint16) error {
|
||||||
addr := &net.UDPAddr{
|
addr := &net.UDPAddr{
|
||||||
IP: net.IP{0, 0, 0, 0},
|
IP: net.IP{0, 0, 0, 0},
|
||||||
Port: int(port),
|
Port: int(port),
|
||||||
@ -22,17 +22,17 @@ func (server *SocksServer) ListenUDP(port uint16) error {
|
|||||||
log.Error("Socks failed to listen UDP on port %d: %v", port, err)
|
log.Error("Socks failed to listen UDP on port %d: %v", port, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
udpAddress = v2net.IPAddress(server.config.IP(), port)
|
udpAddress = v2net.IPAddress(this.config.IP(), port)
|
||||||
|
|
||||||
go server.AcceptPackets(conn)
|
go this.AcceptPackets(conn)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *SocksServer) getUDPAddr() v2net.Address {
|
func (this *SocksServer) getUDPAddr() v2net.Address {
|
||||||
return udpAddress
|
return udpAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
func (this *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
||||||
for {
|
for {
|
||||||
buffer := alloc.NewBuffer()
|
buffer := alloc.NewBuffer()
|
||||||
nBytes, addr, err := conn.ReadFromUDP(buffer.Value)
|
nBytes, addr, err := conn.ReadFromUDP(buffer.Value)
|
||||||
@ -60,12 +60,12 @@ func (server *SocksServer) AcceptPackets(conn *net.UDPConn) error {
|
|||||||
|
|
||||||
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
udpPacket := v2net.NewPacket(request.Destination(), request.Data, false)
|
||||||
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
|
log.Info("Send packet to %s with %d bytes", udpPacket.Destination().String(), request.Data.Len())
|
||||||
go server.handlePacket(conn, udpPacket, addr, request.Address)
|
go this.handlePacket(conn, udpPacket, addr, request.Address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
|
func (this *SocksServer) handlePacket(conn *net.UDPConn, packet v2net.Packet, clientAddr *net.UDPAddr, targetAddr v2net.Address) {
|
||||||
ray := server.dispatcher.DispatchToOutbound(packet)
|
ray := this.dispatcher.DispatchToOutbound(packet)
|
||||||
close(ray.InboundInput())
|
close(ray.InboundInput())
|
||||||
|
|
||||||
for data := range ray.InboundOutput() {
|
for data := range ray.InboundOutput() {
|
||||||
|
@ -44,11 +44,11 @@ type VMessRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Destination is the final destination of this request.
|
// Destination is the final destination of this request.
|
||||||
func (request *VMessRequest) Destination() v2net.Destination {
|
func (this *VMessRequest) Destination() v2net.Destination {
|
||||||
if request.Command == CmdTCP {
|
if this.Command == CmdTCP {
|
||||||
return v2net.NewTCPDestination(request.Address)
|
return v2net.NewTCPDestination(this.Address)
|
||||||
} else {
|
} else {
|
||||||
return v2net.NewUDPDestination(request.Address)
|
return v2net.NewUDPDestination(this.Address)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +65,7 @@ func NewVMessRequestReader(vUserSet user.UserSet) *VMessRequestReader {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Read reads a VMessRequest from a byte stream.
|
// Read reads a VMessRequest from a byte stream.
|
||||||
func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||||
buffer := alloc.NewSmallBuffer()
|
buffer := alloc.NewSmallBuffer()
|
||||||
|
|
||||||
nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:config.IDBytesLen])
|
nBytes, err := v2net.ReadAllBytes(reader, buffer.Value[:config.IDBytesLen])
|
||||||
@ -73,7 +73,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
userObj, timeSec, valid := r.vUserSet.GetUser(buffer.Value[:nBytes])
|
userObj, timeSec, valid := this.vUserSet.GetUser(buffer.Value[:nBytes])
|
||||||
if !valid {
|
if !valid {
|
||||||
return nil, proxyerrors.InvalidAuthentication
|
return nil, proxyerrors.InvalidAuthentication
|
||||||
}
|
}
|
||||||
@ -155,35 +155,35 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToBytes returns a VMessRequest in the form of byte array.
|
// ToBytes returns a VMessRequest in the form of byte array.
|
||||||
func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user.RandomInt64InRange, buffer *alloc.Buffer) (*alloc.Buffer, error) {
|
func (this *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 user.RandomInt64InRange, buffer *alloc.Buffer) (*alloc.Buffer, error) {
|
||||||
if buffer == nil {
|
if buffer == nil {
|
||||||
buffer = alloc.NewSmallBuffer().Clear()
|
buffer = alloc.NewSmallBuffer().Clear()
|
||||||
}
|
}
|
||||||
|
|
||||||
counter := randomRangeInt64(time.Now().Unix(), 30)
|
counter := randomRangeInt64(time.Now().Unix(), 30)
|
||||||
hash := idHash.Hash(request.User.ID().Bytes[:], counter)
|
hash := idHash.Hash(this.User.ID().Bytes[:], counter)
|
||||||
|
|
||||||
buffer.Append(hash)
|
buffer.Append(hash)
|
||||||
|
|
||||||
encryptionBegin := buffer.Len()
|
encryptionBegin := buffer.Len()
|
||||||
|
|
||||||
buffer.AppendBytes(request.Version)
|
buffer.AppendBytes(this.Version)
|
||||||
buffer.Append(request.RequestIV)
|
buffer.Append(this.RequestIV)
|
||||||
buffer.Append(request.RequestKey)
|
buffer.Append(this.RequestKey)
|
||||||
buffer.Append(request.ResponseHeader)
|
buffer.Append(this.ResponseHeader)
|
||||||
buffer.AppendBytes(request.Command)
|
buffer.AppendBytes(this.Command)
|
||||||
buffer.Append(request.Address.PortBytes())
|
buffer.Append(this.Address.PortBytes())
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case request.Address.IsIPv4():
|
case this.Address.IsIPv4():
|
||||||
buffer.AppendBytes(addrTypeIPv4)
|
buffer.AppendBytes(addrTypeIPv4)
|
||||||
buffer.Append(request.Address.IP())
|
buffer.Append(this.Address.IP())
|
||||||
case request.Address.IsIPv6():
|
case this.Address.IsIPv6():
|
||||||
buffer.AppendBytes(addrTypeIPv6)
|
buffer.AppendBytes(addrTypeIPv6)
|
||||||
buffer.Append(request.Address.IP())
|
buffer.Append(this.Address.IP())
|
||||||
case request.Address.IsDomain():
|
case this.Address.IsDomain():
|
||||||
buffer.AppendBytes(addrTypeDomain, byte(len(request.Address.Domain())))
|
buffer.AppendBytes(addrTypeDomain, byte(len(this.Address.Domain())))
|
||||||
buffer.Append([]byte(request.Address.Domain()))
|
buffer.Append([]byte(this.Address.Domain()))
|
||||||
}
|
}
|
||||||
|
|
||||||
encryptionEnd := buffer.Len()
|
encryptionEnd := buffer.Len()
|
||||||
@ -195,7 +195,7 @@ func (request *VMessRequest) ToBytes(idHash user.CounterHash, randomRangeInt64 u
|
|||||||
buffer.AppendBytes(byte(fnvHash>>24), byte(fnvHash>>16), byte(fnvHash>>8), byte(fnvHash))
|
buffer.AppendBytes(byte(fnvHash>>24), byte(fnvHash>>16), byte(fnvHash>>8), byte(fnvHash))
|
||||||
encryptionEnd += 4
|
encryptionEnd += 4
|
||||||
|
|
||||||
aesStream, err := v2crypto.NewAesEncryptionStream(request.User.ID().CmdKey(), user.Int64Hash(counter))
|
aesStream, err := v2crypto.NewAesEncryptionStream(this.User.ID().CmdKey(), user.Int64Hash(counter))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,7 @@ func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSe
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessInboundHandler) Listen(port uint16) error {
|
func (this *VMessInboundHandler) Listen(port uint16) error {
|
||||||
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||||
IP: []byte{0, 0, 0, 0},
|
IP: []byte{0, 0, 0, 0},
|
||||||
Port: int(port),
|
Port: int(port),
|
||||||
@ -41,20 +41,20 @@ func (handler *VMessInboundHandler) Listen(port uint16) error {
|
|||||||
log.Error("Unable to listen tcp port %d: %v", port, err)
|
log.Error("Unable to listen tcp port %d: %v", port, err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
handler.accepting = true
|
this.accepting = true
|
||||||
go handler.AcceptConnections(listener)
|
go this.AcceptConnections(listener)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
|
func (this *VMessInboundHandler) AcceptConnections(listener *net.TCPListener) error {
|
||||||
for handler.accepting {
|
for this.accepting {
|
||||||
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
||||||
connection, err := listener.AcceptTCP()
|
connection, err := listener.AcceptTCP()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Failed to accpet connection: %s", err.Error())
|
log.Error("Failed to accpet connection: %s", err.Error())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
go handler.HandleConnection(connection)
|
go this.HandleConnection(connection)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -62,11 +62,11 @@ func (handler *VMessInboundHandler) AcceptConnections(listener *net.TCPListener)
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
|
func (this *VMessInboundHandler) HandleConnection(connection *net.TCPConn) error {
|
||||||
defer connection.Close()
|
defer connection.Close()
|
||||||
|
|
||||||
connReader := v2net.NewTimeOutReader(16, connection)
|
connReader := v2net.NewTimeOutReader(16, connection)
|
||||||
requestReader := protocol.NewVMessRequestReader(handler.clients)
|
requestReader := protocol.NewVMessRequestReader(this.clients)
|
||||||
|
|
||||||
request, err := requestReader.Read(connReader)
|
request, err := requestReader.Read(connReader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -77,7 +77,7 @@ func (handler *VMessInboundHandler) HandleConnection(connection *net.TCPConn) er
|
|||||||
log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "")
|
log.Access(connection.RemoteAddr().String(), request.Address.String(), log.AccessAccepted, "")
|
||||||
log.Debug("VMessIn: Received request for %s", request.Address.String())
|
log.Debug("VMessIn: Received request for %s", request.Address.String())
|
||||||
|
|
||||||
ray := handler.dispatcher.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
|
ray := this.dispatcher.DispatchToOutbound(v2net.NewPacket(request.Destination(), nil, true))
|
||||||
input := ray.InboundInput()
|
input := ray.InboundInput()
|
||||||
output := ray.InboundOutput()
|
output := ray.InboundOutput()
|
||||||
var readFinish, writeFinish sync.Mutex
|
var readFinish, writeFinish sync.Mutex
|
||||||
@ -139,7 +139,7 @@ func handleOutput(request *protocol.VMessRequest, writer io.Writer, output <-cha
|
|||||||
type VMessInboundHandlerFactory struct {
|
type VMessInboundHandlerFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
func (this *VMessInboundHandlerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||||
config := rawConfig.(config.Inbound)
|
config := rawConfig.(config.Inbound)
|
||||||
|
|
||||||
allowedClients := user.NewTimedUserSet()
|
allowedClients := user.NewTimedUserSet()
|
||||||
|
@ -52,8 +52,8 @@ func pickVNext(serverList []*config.OutboundTarget) (v2net.Destination, config.U
|
|||||||
return vNext.Destination, vNextUser
|
return vNext.Destination, vNextUser
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
func (this *VMessOutboundHandler) Dispatch(firstPacket v2net.Packet, ray ray.OutboundRay) error {
|
||||||
vNextList := handler.vNextList
|
vNextList := this.vNextList
|
||||||
vNextAddress, vNextUser := pickVNext(vNextList)
|
vNextAddress, vNextUser := pickVNext(vNextList)
|
||||||
|
|
||||||
command := protocol.CmdTCP
|
command := protocol.CmdTCP
|
||||||
@ -187,7 +187,7 @@ func handleResponse(conn net.Conn, request *protocol.VMessRequest, output chan<-
|
|||||||
type VMessOutboundHandlerFactory struct {
|
type VMessOutboundHandlerFactory struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (factory *VMessOutboundHandlerFactory) Create(rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
|
func (this *VMessOutboundHandlerFactory) Create(rawConfig interface{}) (connhandler.OutboundConnectionHandler, error) {
|
||||||
vOutConfig := rawConfig.(config.Outbound)
|
vOutConfig := rawConfig.(config.Outbound)
|
||||||
return NewVMessOutboundHandler(vOutConfig.Targets()), nil
|
return NewVMessOutboundHandler(vOutConfig.Targets()), nil
|
||||||
}
|
}
|
||||||
|
@ -21,18 +21,18 @@ type directRay struct {
|
|||||||
Output chan *alloc.Buffer
|
Output chan *alloc.Buffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ray *directRay) OutboundInput() <-chan *alloc.Buffer {
|
func (this *directRay) OutboundInput() <-chan *alloc.Buffer {
|
||||||
return ray.Input
|
return this.Input
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ray *directRay) OutboundOutput() chan<- *alloc.Buffer {
|
func (this *directRay) OutboundOutput() chan<- *alloc.Buffer {
|
||||||
return ray.Output
|
return this.Output
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ray *directRay) InboundInput() chan<- *alloc.Buffer {
|
func (this *directRay) InboundInput() chan<- *alloc.Buffer {
|
||||||
return ray.Input
|
return this.Input
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ray *directRay) InboundOutput() <-chan *alloc.Buffer {
|
func (this *directRay) InboundOutput() <-chan *alloc.Buffer {
|
||||||
return ray.Output
|
return this.Output
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user