mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-02-02 06:15:24 -05:00
refactor common/net.Port
This commit is contained in:
@@ -2,6 +2,7 @@ package connhandler
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
// A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand.
|
||||
@@ -14,5 +15,5 @@ type InboundConnectionHandlerFactory interface {
|
||||
type InboundConnectionHandler interface {
|
||||
// Listen starts a InboundConnectionHandler by listen on a specific port. This method is called
|
||||
// exactly once during runtime.
|
||||
Listen(port uint16) error
|
||||
Listen(port v2net.Port) error
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||
)
|
||||
|
||||
type DokodemoConfig struct {
|
||||
Host string `json:"address"`
|
||||
Port int `json:"port"`
|
||||
Port v2net.Port `json:"port"`
|
||||
Network *v2netjson.NetworkList `json:"network"`
|
||||
Timeout int `json:"timeout"`
|
||||
}
|
||||
|
||||
@@ -27,14 +27,14 @@ func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfi
|
||||
}
|
||||
ip := net.ParseIP(config.Host)
|
||||
if ip != nil {
|
||||
d.address = v2net.IPAddress(ip, uint16(config.Port))
|
||||
d.address = v2net.IPAddress(ip, config.Port)
|
||||
} else {
|
||||
d.address = v2net.DomainAddress(config.Host, uint16(config.Port))
|
||||
d.address = v2net.DomainAddress(config.Host, config.Port)
|
||||
}
|
||||
return d
|
||||
}
|
||||
|
||||
func (this *DokodemoDoor) Listen(port uint16) error {
|
||||
func (this *DokodemoDoor) Listen(port v2net.Port) error {
|
||||
this.accepting = true
|
||||
|
||||
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
|
||||
@@ -52,7 +52,7 @@ func (this *DokodemoDoor) Listen(port uint16) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *DokodemoDoor) ListenUDP(port uint16) error {
|
||||
func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
|
||||
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
|
||||
IP: []byte{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
@@ -88,7 +88,7 @@ func (this *DokodemoDoor) handleUDPPackets(udpConn *net.UDPConn) {
|
||||
}
|
||||
}
|
||||
|
||||
func (this *DokodemoDoor) ListenTCP(port uint16) error {
|
||||
func (this *DokodemoDoor) ListenTCP(port v2net.Port) error {
|
||||
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||
IP: []byte{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestDokodemoTCP(t *testing.T) {
|
||||
ProtocolValue: "dokodemo-door",
|
||||
SettingsValue: &json.DokodemoConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: int(port),
|
||||
Port: port,
|
||||
Network: &networkList,
|
||||
Timeout: 0,
|
||||
},
|
||||
@@ -105,7 +105,7 @@ func TestDokodemoUDP(t *testing.T) {
|
||||
ProtocolValue: "dokodemo-door",
|
||||
SettingsValue: &json.DokodemoConfig{
|
||||
Host: "127.0.0.1",
|
||||
Port: int(port),
|
||||
Port: port,
|
||||
Network: &networkList,
|
||||
Timeout: 0,
|
||||
},
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
"github.com/v2ray/v2ray-core/common/log"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
jsonconfig "github.com/v2ray/v2ray-core/proxy/http/config/json"
|
||||
)
|
||||
|
||||
@@ -22,7 +23,7 @@ func NewHttpProxyServer(dispatcher app.PacketDispatcher, config *jsonconfig.Http
|
||||
}
|
||||
}
|
||||
|
||||
func (server *HttpProxyServer) Listen(port uint16) error {
|
||||
func (server *HttpProxyServer) Listen(port v2net.Port) error {
|
||||
_, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||
IP: []byte{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
@@ -57,7 +56,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
|
||||
if buffer.Value[0] == socks4Version {
|
||||
auth4.Version = buffer.Value[0]
|
||||
auth4.Command = buffer.Value[1]
|
||||
auth4.Port = binary.BigEndian.Uint16(buffer.Value[2:4])
|
||||
auth4.Port = v2net.PortFromBytes(buffer.Value[2:4])
|
||||
copy(auth4.IP[:], buffer.Value[4:8])
|
||||
err = Socks4Downgrade
|
||||
return
|
||||
@@ -184,7 +183,7 @@ type Socks5Request struct {
|
||||
IPv4 [4]byte
|
||||
Domain string
|
||||
IPv6 [16]byte
|
||||
Port uint16
|
||||
Port v2net.Port
|
||||
}
|
||||
|
||||
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||
@@ -256,7 +255,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
request.Port = binary.BigEndian.Uint16(buffer.Value[:2])
|
||||
request.Port = v2net.PortFromBytes(buffer.Value[:2])
|
||||
return
|
||||
}
|
||||
|
||||
@@ -294,7 +293,7 @@ type Socks5Response struct {
|
||||
IPv4 [4]byte
|
||||
Domain string
|
||||
IPv6 [16]byte
|
||||
Port uint16
|
||||
Port v2net.Port
|
||||
}
|
||||
|
||||
func NewSocks5Response() *Socks5Response {
|
||||
@@ -329,5 +328,5 @@ func (r *Socks5Response) Write(buffer *alloc.Buffer) {
|
||||
case 0x04:
|
||||
buffer.Append(r.IPv6[:])
|
||||
}
|
||||
buffer.AppendBytes(byte(r.Port>>8), byte(r.Port))
|
||||
buffer.Append(r.Port.Bytes())
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -13,7 +14,7 @@ var (
|
||||
type Socks4AuthenticationRequest struct {
|
||||
Version byte
|
||||
Command byte
|
||||
Port uint16
|
||||
Port v2net.Port
|
||||
IP [4]byte
|
||||
}
|
||||
|
||||
@@ -23,10 +24,10 @@ type Socks4AuthenticationResponse struct {
|
||||
ip []byte
|
||||
}
|
||||
|
||||
func NewSocks4AuthenticationResponse(result byte, port uint16, ip []byte) *Socks4AuthenticationResponse {
|
||||
func NewSocks4AuthenticationResponse(result byte, port v2net.Port, ip []byte) *Socks4AuthenticationResponse {
|
||||
return &Socks4AuthenticationResponse{
|
||||
result: result,
|
||||
port: port,
|
||||
port: port.Value(),
|
||||
ip: ip,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
)
|
||||
@@ -22,7 +24,7 @@ func TestSocks4AuthenticationRequestRead(t *testing.T) {
|
||||
assert.Error(err).Equals(Socks4Downgrade)
|
||||
assert.Byte(request4.Version).Named("Version").Equals(0x04)
|
||||
assert.Byte(request4.Command).Named("Command").Equals(0x01)
|
||||
assert.Uint16(request4.Port).Named("Port").Equals(53)
|
||||
v2netassert.Port(request4.Port).Named("Port").Equals(v2net.Port(53))
|
||||
assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72})
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
v2netassert "github.com/v2ray/v2ray-core/common/net/testing/assert"
|
||||
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||
"github.com/v2ray/v2ray-core/testing/assert"
|
||||
"github.com/v2ray/v2ray-core/transport"
|
||||
@@ -68,7 +70,7 @@ func TestRequestRead(t *testing.T) {
|
||||
assert.Byte(request.Command).Named("Command").Equals(0x01)
|
||||
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
|
||||
assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
|
||||
assert.Uint16(request.Port).Named("Port").Equals(53)
|
||||
v2netassert.Port(request.Port).Named("Port").Equals(v2net.Port(53))
|
||||
}
|
||||
|
||||
func TestResponseWrite(t *testing.T) {
|
||||
@@ -81,7 +83,7 @@ func TestResponseWrite(t *testing.T) {
|
||||
[4]byte{0x72, 0x72, 0x72, 0x72},
|
||||
"",
|
||||
[16]byte{},
|
||||
uint16(53),
|
||||
v2net.Port(53),
|
||||
}
|
||||
buffer := alloc.NewSmallBuffer().Clear()
|
||||
defer buffer.Release()
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
|
||||
"github.com/v2ray/v2ray-core/common/alloc"
|
||||
@@ -56,7 +55,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
|
||||
return nil, transport.CorruptedPacket
|
||||
}
|
||||
ip := packet[4:8]
|
||||
port := binary.BigEndian.Uint16(packet[8:10])
|
||||
port := v2net.PortFromBytes(packet[8:10])
|
||||
request.Address = v2net.IPAddress(ip, port)
|
||||
dataBegin = 10
|
||||
case AddrTypeIPv6:
|
||||
@@ -64,7 +63,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
|
||||
return nil, transport.CorruptedPacket
|
||||
}
|
||||
ip := packet[4:20]
|
||||
port := binary.BigEndian.Uint16(packet[20:22])
|
||||
port := v2net.PortFromBytes(packet[20:22])
|
||||
request.Address = v2net.IPAddress(ip, port)
|
||||
dataBegin = 22
|
||||
case AddrTypeDomain:
|
||||
@@ -73,7 +72,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
|
||||
return nil, transport.CorruptedPacket
|
||||
}
|
||||
domain := string(packet[5 : 5+domainLength])
|
||||
port := binary.BigEndian.Uint16(packet[5+domainLength : 5+domainLength+2])
|
||||
port := v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
|
||||
request.Address = v2net.DomainAddress(domain, port)
|
||||
dataBegin = 5 + domainLength + 2
|
||||
default:
|
||||
|
||||
@@ -36,7 +36,7 @@ func NewSocksServer(dispatcher app.PacketDispatcher, config *jsonconfig.SocksCon
|
||||
}
|
||||
}
|
||||
|
||||
func (this *SocksServer) Listen(port uint16) error {
|
||||
func (this *SocksServer) Listen(port v2net.Port) error {
|
||||
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||
IP: []byte{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
@@ -145,7 +145,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
|
||||
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
|
||||
response := protocol.NewSocks5Response()
|
||||
response.Error = protocol.ErrorCommandNotSupported
|
||||
response.Port = uint16(0)
|
||||
response.Port = v2net.Port(0)
|
||||
response.SetIPv4([]byte{0, 0, 0, 0})
|
||||
|
||||
responseBuffer := alloc.NewSmallBuffer().Clear()
|
||||
@@ -164,7 +164,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
|
||||
response.Error = protocol.ErrorSuccess
|
||||
|
||||
// Some SOCKS software requires a value other than dest. Let's fake one:
|
||||
response.Port = uint16(1717)
|
||||
response.Port = v2net.Port(1717)
|
||||
response.SetIPv4([]byte{0, 0, 0, 0})
|
||||
|
||||
responseBuffer := alloc.NewSmallBuffer().Clear()
|
||||
@@ -193,7 +193,7 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer
|
||||
|
||||
udpAddr := this.getUDPAddr()
|
||||
|
||||
response.Port = udpAddr.Port().Value()
|
||||
response.Port = udpAddr.Port()
|
||||
switch {
|
||||
case udpAddr.IsIPv4():
|
||||
response.SetIPv4(udpAddr.IP())
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
|
||||
var udpAddress v2net.Address
|
||||
|
||||
func (this *SocksServer) ListenUDP(port uint16) error {
|
||||
func (this *SocksServer) ListenUDP(port v2net.Port) error {
|
||||
addr := &net.UDPAddr{
|
||||
IP: net.IP{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
|
||||
@@ -10,13 +10,13 @@ import (
|
||||
)
|
||||
|
||||
type InboundConnectionHandler struct {
|
||||
Port uint16
|
||||
Port v2net.Port
|
||||
Dispatcher app.PacketDispatcher
|
||||
ConnInput io.Reader
|
||||
ConnOutput io.Writer
|
||||
}
|
||||
|
||||
func (this *InboundConnectionHandler) Listen(port uint16) error {
|
||||
func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
|
||||
this.Port = port
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
type RawConfigTarget struct {
|
||||
Address string `json:"address"`
|
||||
Port uint16 `json:"port"`
|
||||
Port v2net.Port `json:"port"`
|
||||
Users []*ConfigUser `json:"users"`
|
||||
}
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ func NewVMessInboundHandler(dispatcher app.PacketDispatcher, clients user.UserSe
|
||||
}
|
||||
}
|
||||
|
||||
func (this *VMessInboundHandler) Listen(port uint16) error {
|
||||
func (this *VMessInboundHandler) Listen(port v2net.Port) error {
|
||||
listener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||
IP: []byte{0, 0, 0, 0},
|
||||
Port: int(port),
|
||||
|
||||
@@ -106,7 +106,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
||||
request.ResponseHeader = buffer.Value[33:37] // 4 bytes
|
||||
request.Command = buffer.Value[37]
|
||||
|
||||
port := binary.BigEndian.Uint16(buffer.Value[38:40])
|
||||
port := v2net.PortFromBytes(buffer.Value[38:40])
|
||||
|
||||
switch buffer.Value[40] {
|
||||
case addrTypeIPv4:
|
||||
|
||||
Reference in New Issue
Block a user