mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-30 05:56:54 -05:00
refactor common/net.Port
This commit is contained in:
parent
fa7c1069bc
commit
ae056714db
@ -30,11 +30,11 @@ func allZeros(data []byte) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IPAddress creates an Address with given IP and port.
|
// IPAddress creates an Address with given IP and port.
|
||||||
func IPAddress(ip []byte, port uint16) Address {
|
func IPAddress(ip []byte, port Port) Address {
|
||||||
switch len(ip) {
|
switch len(ip) {
|
||||||
case net.IPv4len:
|
case net.IPv4len:
|
||||||
return &IPv4Address{
|
return &IPv4Address{
|
||||||
port: Port(port),
|
port: port,
|
||||||
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
ip: [4]byte{ip[0], ip[1], ip[2], ip[3]},
|
||||||
}
|
}
|
||||||
case net.IPv6len:
|
case net.IPv6len:
|
||||||
@ -57,10 +57,10 @@ func IPAddress(ip []byte, port uint16) Address {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DomainAddress creates an Address with given domain and port.
|
// DomainAddress creates an Address with given domain and port.
|
||||||
func DomainAddress(domain string, port uint16) Address {
|
func DomainAddress(domain string, port Port) Address {
|
||||||
return &DomainAddressImpl{
|
return &DomainAddressImpl{
|
||||||
domain: domain,
|
domain: domain,
|
||||||
port: Port(port),
|
port: port,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,8 +14,8 @@ func TestIPv4Address(t *testing.T) {
|
|||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
ip := []byte{byte(1), byte(2), byte(3), byte(4)}
|
||||||
port := v2net.NewPort(80)
|
port := v2net.Port(80)
|
||||||
addr := v2net.IPAddress(ip, port.Value())
|
addr := v2net.IPAddress(ip, port)
|
||||||
|
|
||||||
v2netassert.Address(addr).IsIPv4()
|
v2netassert.Address(addr).IsIPv4()
|
||||||
v2netassert.Address(addr).IsNotIPv6()
|
v2netassert.Address(addr).IsNotIPv6()
|
||||||
@ -34,8 +34,8 @@ func TestIPv6Address(t *testing.T) {
|
|||||||
byte(1), byte(2), byte(3), byte(4),
|
byte(1), byte(2), byte(3), byte(4),
|
||||||
byte(1), byte(2), byte(3), byte(4),
|
byte(1), byte(2), byte(3), byte(4),
|
||||||
}
|
}
|
||||||
port := v2net.NewPort(443)
|
port := v2net.Port(443)
|
||||||
addr := v2net.IPAddress(ip, port.Value())
|
addr := v2net.IPAddress(ip, port)
|
||||||
|
|
||||||
v2netassert.Address(addr).IsIPv6()
|
v2netassert.Address(addr).IsIPv6()
|
||||||
v2netassert.Address(addr).IsNotIPv4()
|
v2netassert.Address(addr).IsNotIPv4()
|
||||||
@ -49,8 +49,8 @@ func TestDomainAddress(t *testing.T) {
|
|||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
domain := "v2ray.com"
|
domain := "v2ray.com"
|
||||||
port := v2net.NewPort(443)
|
port := v2net.Port(443)
|
||||||
addr := v2net.DomainAddress(domain, port.Value())
|
addr := v2net.DomainAddress(domain, port)
|
||||||
|
|
||||||
v2netassert.Address(addr).IsDomain()
|
v2netassert.Address(addr).IsDomain()
|
||||||
v2netassert.Address(addr).IsNotIPv6()
|
v2netassert.Address(addr).IsNotIPv6()
|
||||||
@ -64,8 +64,8 @@ func TestNetIPv4Address(t *testing.T) {
|
|||||||
v2testing.Current(t)
|
v2testing.Current(t)
|
||||||
|
|
||||||
ip := net.IPv4(1, 2, 3, 4)
|
ip := net.IPv4(1, 2, 3, 4)
|
||||||
port := v2net.NewPort(80)
|
port := v2net.Port(80)
|
||||||
addr := v2net.IPAddress(ip, port.Value())
|
addr := v2net.IPAddress(ip, port)
|
||||||
v2netassert.Address(addr).IsIPv4()
|
v2netassert.Address(addr).IsIPv4()
|
||||||
assert.String(addr).Equals("1.2.3.4:80")
|
assert.String(addr).Equals("1.2.3.4:80")
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import (
|
|||||||
|
|
||||||
type Port uint16
|
type Port uint16
|
||||||
|
|
||||||
func NewPort(port int) Port {
|
func PortFromBytes(port []byte) Port {
|
||||||
return Port(uint16(port))
|
return Port(uint16(port[0])<<8 + uint16(port[1]))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this Port) Value() uint16 {
|
func (this Port) Value() uint16 {
|
||||||
|
@ -11,7 +11,7 @@ func Address(value v2net.Address) *AddressSubject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AddressSubject struct {
|
type AddressSubject struct {
|
||||||
*assert.Subject
|
assert.Subject
|
||||||
value v2net.Address
|
value v2net.Address
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ func Destination(value v2net.Destination) *DestinationSubject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type DestinationSubject struct {
|
type DestinationSubject struct {
|
||||||
*assert.Subject
|
assert.Subject
|
||||||
value v2net.Destination
|
value v2net.Destination
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package assert
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
|
"github.com/v2ray/v2ray-core/common/serial"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -10,7 +11,7 @@ func Port(value v2net.Port) *PortSubject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PortSubject struct {
|
type PortSubject struct {
|
||||||
*assert.Subject
|
assert.Subject
|
||||||
value v2net.Port
|
value v2net.Port
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,3 +41,9 @@ func (subject *PortSubject) LessThan(expectation v2net.Port) {
|
|||||||
subject.Fail(subject.DisplayString(), "is less than", expectation)
|
subject.Fail(subject.DisplayString(), "is less than", expectation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (subject *PortSubject) IsValid() {
|
||||||
|
if subject.value == 0 {
|
||||||
|
subject.Fail(subject.DisplayString(), "is", serial.StringLiteral("a valid port"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,12 +2,14 @@ package testing
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
port = int32(30000)
|
port = int32(30000)
|
||||||
)
|
)
|
||||||
|
|
||||||
func PickPort() uint16 {
|
func PickPort() v2net.Port {
|
||||||
return uint16(atomic.AddInt32(&port, 1))
|
return v2net.Port(uint16(atomic.AddInt32(&port, 1)))
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package connhandler
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/v2ray/v2ray-core/app"
|
"github.com/v2ray/v2ray-core/app"
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand.
|
// A InboundConnectionHandlerFactory creates InboundConnectionHandler on demand.
|
||||||
@ -14,5 +15,5 @@ type InboundConnectionHandlerFactory interface {
|
|||||||
type InboundConnectionHandler interface {
|
type InboundConnectionHandler interface {
|
||||||
// Listen starts a InboundConnectionHandler by listen on a specific port. This method is called
|
// Listen starts a InboundConnectionHandler by listen on a specific port. This method is called
|
||||||
// exactly once during runtime.
|
// exactly once during runtime.
|
||||||
Listen(port uint16) error
|
Listen(port v2net.Port) error
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,14 @@
|
|||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
|
||||||
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
"github.com/v2ray/v2ray-core/proxy/common/config/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DokodemoConfig struct {
|
type DokodemoConfig struct {
|
||||||
Host string `json:"address"`
|
Host string `json:"address"`
|
||||||
Port int `json:"port"`
|
Port v2net.Port `json:"port"`
|
||||||
Network *v2netjson.NetworkList `json:"network"`
|
Network *v2netjson.NetworkList `json:"network"`
|
||||||
Timeout int `json:"timeout"`
|
Timeout int `json:"timeout"`
|
||||||
}
|
}
|
||||||
|
@ -27,14 +27,14 @@ func NewDokodemoDoor(dispatcher app.PacketDispatcher, config *json.DokodemoConfi
|
|||||||
}
|
}
|
||||||
ip := net.ParseIP(config.Host)
|
ip := net.ParseIP(config.Host)
|
||||||
if ip != nil {
|
if ip != nil {
|
||||||
d.address = v2net.IPAddress(ip, uint16(config.Port))
|
d.address = v2net.IPAddress(ip, config.Port)
|
||||||
} else {
|
} else {
|
||||||
d.address = v2net.DomainAddress(config.Host, uint16(config.Port))
|
d.address = v2net.DomainAddress(config.Host, config.Port)
|
||||||
}
|
}
|
||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DokodemoDoor) Listen(port uint16) error {
|
func (this *DokodemoDoor) Listen(port v2net.Port) error {
|
||||||
this.accepting = true
|
this.accepting = true
|
||||||
|
|
||||||
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
|
if this.config.Network.HasNetwork(v2net.TCPNetwork) {
|
||||||
@ -52,7 +52,7 @@ func (this *DokodemoDoor) Listen(port uint16) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *DokodemoDoor) ListenUDP(port uint16) error {
|
func (this *DokodemoDoor) ListenUDP(port v2net.Port) error {
|
||||||
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
|
udpConn, err := net.ListenUDP("udp", &net.UDPAddr{
|
||||||
IP: []byte{0, 0, 0, 0},
|
IP: []byte{0, 0, 0, 0},
|
||||||
Port: int(port),
|
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{
|
tcpListener, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||||
IP: []byte{0, 0, 0, 0},
|
IP: []byte{0, 0, 0, 0},
|
||||||
Port: int(port),
|
Port: int(port),
|
||||||
|
@ -43,7 +43,7 @@ func TestDokodemoTCP(t *testing.T) {
|
|||||||
ProtocolValue: "dokodemo-door",
|
ProtocolValue: "dokodemo-door",
|
||||||
SettingsValue: &json.DokodemoConfig{
|
SettingsValue: &json.DokodemoConfig{
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: int(port),
|
Port: port,
|
||||||
Network: &networkList,
|
Network: &networkList,
|
||||||
Timeout: 0,
|
Timeout: 0,
|
||||||
},
|
},
|
||||||
@ -105,7 +105,7 @@ func TestDokodemoUDP(t *testing.T) {
|
|||||||
ProtocolValue: "dokodemo-door",
|
ProtocolValue: "dokodemo-door",
|
||||||
SettingsValue: &json.DokodemoConfig{
|
SettingsValue: &json.DokodemoConfig{
|
||||||
Host: "127.0.0.1",
|
Host: "127.0.0.1",
|
||||||
Port: int(port),
|
Port: port,
|
||||||
Network: &networkList,
|
Network: &networkList,
|
||||||
Timeout: 0,
|
Timeout: 0,
|
||||||
},
|
},
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"github.com/v2ray/v2ray-core/app"
|
"github.com/v2ray/v2ray-core/app"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"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"
|
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{
|
_, err := net.ListenTCP("tcp", &net.TCPAddr{
|
||||||
IP: []byte{0, 0, 0, 0},
|
IP: []byte{0, 0, 0, 0},
|
||||||
Port: int(port),
|
Port: int(port),
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"github.com/v2ray/v2ray-core/common/alloc"
|
||||||
@ -57,7 +56,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
|
|||||||
if buffer.Value[0] == socks4Version {
|
if buffer.Value[0] == socks4Version {
|
||||||
auth4.Version = buffer.Value[0]
|
auth4.Version = buffer.Value[0]
|
||||||
auth4.Command = buffer.Value[1]
|
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])
|
copy(auth4.IP[:], buffer.Value[4:8])
|
||||||
err = Socks4Downgrade
|
err = Socks4Downgrade
|
||||||
return
|
return
|
||||||
@ -184,7 +183,7 @@ type Socks5Request struct {
|
|||||||
IPv4 [4]byte
|
IPv4 [4]byte
|
||||||
Domain string
|
Domain string
|
||||||
IPv6 [16]byte
|
IPv6 [16]byte
|
||||||
Port uint16
|
Port v2net.Port
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
||||||
@ -256,7 +255,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
request.Port = binary.BigEndian.Uint16(buffer.Value[:2])
|
request.Port = v2net.PortFromBytes(buffer.Value[:2])
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,7 +293,7 @@ type Socks5Response struct {
|
|||||||
IPv4 [4]byte
|
IPv4 [4]byte
|
||||||
Domain string
|
Domain string
|
||||||
IPv6 [16]byte
|
IPv6 [16]byte
|
||||||
Port uint16
|
Port v2net.Port
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSocks5Response() *Socks5Response {
|
func NewSocks5Response() *Socks5Response {
|
||||||
@ -329,5 +328,5 @@ func (r *Socks5Response) Write(buffer *alloc.Buffer) {
|
|||||||
case 0x04:
|
case 0x04:
|
||||||
buffer.Append(r.IPv6[:])
|
buffer.Append(r.IPv6[:])
|
||||||
}
|
}
|
||||||
buffer.AppendBytes(byte(r.Port>>8), byte(r.Port))
|
buffer.Append(r.Port.Bytes())
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"github.com/v2ray/v2ray-core/common/alloc"
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -13,7 +14,7 @@ var (
|
|||||||
type Socks4AuthenticationRequest struct {
|
type Socks4AuthenticationRequest struct {
|
||||||
Version byte
|
Version byte
|
||||||
Command byte
|
Command byte
|
||||||
Port uint16
|
Port v2net.Port
|
||||||
IP [4]byte
|
IP [4]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,10 +24,10 @@ type Socks4AuthenticationResponse struct {
|
|||||||
ip []byte
|
ip []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSocks4AuthenticationResponse(result byte, port uint16, ip []byte) *Socks4AuthenticationResponse {
|
func NewSocks4AuthenticationResponse(result byte, port v2net.Port, ip []byte) *Socks4AuthenticationResponse {
|
||||||
return &Socks4AuthenticationResponse{
|
return &Socks4AuthenticationResponse{
|
||||||
result: result,
|
result: result,
|
||||||
port: port,
|
port: port.Value(),
|
||||||
ip: ip,
|
ip: ip,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"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"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
)
|
)
|
||||||
@ -22,7 +24,7 @@ func TestSocks4AuthenticationRequestRead(t *testing.T) {
|
|||||||
assert.Error(err).Equals(Socks4Downgrade)
|
assert.Error(err).Equals(Socks4Downgrade)
|
||||||
assert.Byte(request4.Version).Named("Version").Equals(0x04)
|
assert.Byte(request4.Version).Named("Version").Equals(0x04)
|
||||||
assert.Byte(request4.Command).Named("Command").Equals(0x01)
|
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})
|
assert.Bytes(request4.IP[:]).Named("IP").Equals([]byte{0x72, 0x72, 0x72, 0x72})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,6 +6,8 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"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"
|
v2testing "github.com/v2ray/v2ray-core/testing"
|
||||||
"github.com/v2ray/v2ray-core/testing/assert"
|
"github.com/v2ray/v2ray-core/testing/assert"
|
||||||
"github.com/v2ray/v2ray-core/transport"
|
"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.Command).Named("Command").Equals(0x01)
|
||||||
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
|
assert.Byte(request.AddrType).Named("Address Type").Equals(0x01)
|
||||||
assert.Bytes(request.IPv4[:]).Named("IPv4").Equals([]byte{0x72, 0x72, 0x72, 0x72})
|
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) {
|
func TestResponseWrite(t *testing.T) {
|
||||||
@ -81,7 +83,7 @@ func TestResponseWrite(t *testing.T) {
|
|||||||
[4]byte{0x72, 0x72, 0x72, 0x72},
|
[4]byte{0x72, 0x72, 0x72, 0x72},
|
||||||
"",
|
"",
|
||||||
[16]byte{},
|
[16]byte{},
|
||||||
uint16(53),
|
v2net.Port(53),
|
||||||
}
|
}
|
||||||
buffer := alloc.NewSmallBuffer().Clear()
|
buffer := alloc.NewSmallBuffer().Clear()
|
||||||
defer buffer.Release()
|
defer buffer.Release()
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
"github.com/v2ray/v2ray-core/common/alloc"
|
"github.com/v2ray/v2ray-core/common/alloc"
|
||||||
@ -56,7 +55,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
|
|||||||
return nil, transport.CorruptedPacket
|
return nil, transport.CorruptedPacket
|
||||||
}
|
}
|
||||||
ip := packet[4:8]
|
ip := packet[4:8]
|
||||||
port := binary.BigEndian.Uint16(packet[8:10])
|
port := v2net.PortFromBytes(packet[8:10])
|
||||||
request.Address = v2net.IPAddress(ip, port)
|
request.Address = v2net.IPAddress(ip, port)
|
||||||
dataBegin = 10
|
dataBegin = 10
|
||||||
case AddrTypeIPv6:
|
case AddrTypeIPv6:
|
||||||
@ -64,7 +63,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
|
|||||||
return nil, transport.CorruptedPacket
|
return nil, transport.CorruptedPacket
|
||||||
}
|
}
|
||||||
ip := packet[4:20]
|
ip := packet[4:20]
|
||||||
port := binary.BigEndian.Uint16(packet[20:22])
|
port := v2net.PortFromBytes(packet[20:22])
|
||||||
request.Address = v2net.IPAddress(ip, port)
|
request.Address = v2net.IPAddress(ip, port)
|
||||||
dataBegin = 22
|
dataBegin = 22
|
||||||
case AddrTypeDomain:
|
case AddrTypeDomain:
|
||||||
@ -73,7 +72,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
|
|||||||
return nil, transport.CorruptedPacket
|
return nil, transport.CorruptedPacket
|
||||||
}
|
}
|
||||||
domain := string(packet[5 : 5+domainLength])
|
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)
|
request.Address = v2net.DomainAddress(domain, port)
|
||||||
dataBegin = 5 + domainLength + 2
|
dataBegin = 5 + domainLength + 2
|
||||||
default:
|
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{
|
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),
|
||||||
@ -145,7 +145,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
|
|||||||
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
|
if request.Command == protocol.CmdBind || request.Command == protocol.CmdUdpAssociate {
|
||||||
response := protocol.NewSocks5Response()
|
response := protocol.NewSocks5Response()
|
||||||
response.Error = protocol.ErrorCommandNotSupported
|
response.Error = protocol.ErrorCommandNotSupported
|
||||||
response.Port = uint16(0)
|
response.Port = v2net.Port(0)
|
||||||
response.SetIPv4([]byte{0, 0, 0, 0})
|
response.SetIPv4([]byte{0, 0, 0, 0})
|
||||||
|
|
||||||
responseBuffer := alloc.NewSmallBuffer().Clear()
|
responseBuffer := alloc.NewSmallBuffer().Clear()
|
||||||
@ -164,7 +164,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
|
|||||||
response.Error = protocol.ErrorSuccess
|
response.Error = protocol.ErrorSuccess
|
||||||
|
|
||||||
// Some SOCKS software requires a value other than dest. Let's fake one:
|
// 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})
|
response.SetIPv4([]byte{0, 0, 0, 0})
|
||||||
|
|
||||||
responseBuffer := alloc.NewSmallBuffer().Clear()
|
responseBuffer := alloc.NewSmallBuffer().Clear()
|
||||||
@ -193,7 +193,7 @@ func (this *SocksServer) handleUDP(reader *v2net.TimeOutReader, writer io.Writer
|
|||||||
|
|
||||||
udpAddr := this.getUDPAddr()
|
udpAddr := this.getUDPAddr()
|
||||||
|
|
||||||
response.Port = udpAddr.Port().Value()
|
response.Port = udpAddr.Port()
|
||||||
switch {
|
switch {
|
||||||
case udpAddr.IsIPv4():
|
case udpAddr.IsIPv4():
|
||||||
response.SetIPv4(udpAddr.IP())
|
response.SetIPv4(udpAddr.IP())
|
||||||
|
@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
var udpAddress v2net.Address
|
var udpAddress v2net.Address
|
||||||
|
|
||||||
func (this *SocksServer) ListenUDP(port uint16) error {
|
func (this *SocksServer) ListenUDP(port v2net.Port) 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),
|
||||||
|
@ -10,13 +10,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type InboundConnectionHandler struct {
|
type InboundConnectionHandler struct {
|
||||||
Port uint16
|
Port v2net.Port
|
||||||
Dispatcher app.PacketDispatcher
|
Dispatcher app.PacketDispatcher
|
||||||
ConnInput io.Reader
|
ConnInput io.Reader
|
||||||
ConnOutput io.Writer
|
ConnOutput io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (this *InboundConnectionHandler) Listen(port uint16) error {
|
func (this *InboundConnectionHandler) Listen(port v2net.Port) error {
|
||||||
this.Port = port
|
this.Port = port
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
|
|
||||||
type RawConfigTarget struct {
|
type RawConfigTarget struct {
|
||||||
Address string `json:"address"`
|
Address string `json:"address"`
|
||||||
Port uint16 `json:"port"`
|
Port v2net.Port `json:"port"`
|
||||||
Users []*ConfigUser `json:"users"`
|
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{
|
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),
|
||||||
|
@ -106,7 +106,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
|
|||||||
request.ResponseHeader = buffer.Value[33:37] // 4 bytes
|
request.ResponseHeader = buffer.Value[33:37] // 4 bytes
|
||||||
request.Command = buffer.Value[37]
|
request.Command = buffer.Value[37]
|
||||||
|
|
||||||
port := binary.BigEndian.Uint16(buffer.Value[38:40])
|
port := v2net.PortFromBytes(buffer.Value[38:40])
|
||||||
|
|
||||||
switch buffer.Value[40] {
|
switch buffer.Value[40] {
|
||||||
case addrTypeIPv4:
|
case addrTypeIPv4:
|
||||||
|
@ -27,7 +27,7 @@ type OutboundDetourConfig interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type PointConfig interface {
|
type PointConfig interface {
|
||||||
Port() uint16
|
Port() v2net.Port
|
||||||
LogConfig() LogConfig
|
LogConfig() LogConfig
|
||||||
RouterConfig() routerconfig.RouterConfig
|
RouterConfig() routerconfig.RouterConfig
|
||||||
InboundConfig() ConnectionConfig
|
InboundConfig() ConnectionConfig
|
||||||
|
@ -8,13 +8,14 @@ import (
|
|||||||
routerconfig "github.com/v2ray/v2ray-core/app/router/config"
|
routerconfig "github.com/v2ray/v2ray-core/app/router/config"
|
||||||
routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json"
|
routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
|
||||||
"github.com/v2ray/v2ray-core/shell/point/config"
|
"github.com/v2ray/v2ray-core/shell/point/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config is the config for Point server.
|
// Config is the config for Point server.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PortValue uint16 `json:"port"` // Port of this Point server.
|
PortValue v2net.Port `json:"port"` // Port of this Point server.
|
||||||
LogConfigValue *LogConfig `json:"log"`
|
LogConfigValue *LogConfig `json:"log"`
|
||||||
RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"`
|
RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"`
|
||||||
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
||||||
@ -23,7 +24,7 @@ type Config struct {
|
|||||||
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
|
OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) Port() uint16 {
|
func (config *Config) Port() v2net.Port {
|
||||||
return config.PortValue
|
return config.PortValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,7 +23,7 @@ func TestClientSampleConfig(t *testing.T) {
|
|||||||
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
|
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json"))
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
assert.Uint16(pointConfig.Port()).Positive()
|
assert.Uint16(pointConfig.Port().Value()).Positive()
|
||||||
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
|
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
|
||||||
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
|
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ func TestServerSampleConfig(t *testing.T) {
|
|||||||
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
|
pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json"))
|
||||||
assert.Error(err).IsNil()
|
assert.Error(err).IsNil()
|
||||||
|
|
||||||
assert.Uint16(pointConfig.Port()).Positive()
|
assert.Uint16(pointConfig.Port().Value()).Positive()
|
||||||
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
|
assert.Pointer(pointConfig.InboundConfig()).IsNotNil()
|
||||||
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
|
assert.Pointer(pointConfig.OutboundConfig()).IsNotNil()
|
||||||
|
|
||||||
|
@ -59,7 +59,7 @@ func (config *LogConfig) AccessLog() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
PortValue uint16
|
PortValue v2net.Port
|
||||||
LogConfigValue *LogConfig
|
LogConfigValue *LogConfig
|
||||||
RouterConfigValue routerconfig.RouterConfig
|
RouterConfigValue routerconfig.RouterConfig
|
||||||
InboundConfigValue *ConnectionConfig
|
InboundConfigValue *ConnectionConfig
|
||||||
@ -68,7 +68,7 @@ type Config struct {
|
|||||||
OutboundDetoursValue []*OutboundDetourConfig
|
OutboundDetoursValue []*OutboundDetourConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func (config *Config) Port() uint16 {
|
func (config *Config) Port() v2net.Port {
|
||||||
return config.PortValue
|
return config.PortValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ func (this *InboundDetourHandler) Initialize() error {
|
|||||||
func (this *InboundDetourHandler) Start() error {
|
func (this *InboundDetourHandler) Start() error {
|
||||||
for _, ich := range this.ich {
|
for _, ich := range this.ich {
|
||||||
return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
return retry.Timed(100 /* times */, 100 /* ms */).On(func() error {
|
||||||
err := ich.handler.Listen(ich.port.Value())
|
err := ich.handler.Listen(ich.port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
|
|
||||||
// Point is an single server in V2Ray system.
|
// Point is an single server in V2Ray system.
|
||||||
type Point struct {
|
type Point struct {
|
||||||
port uint16
|
port v2net.Port
|
||||||
ich connhandler.InboundConnectionHandler
|
ich connhandler.InboundConnectionHandler
|
||||||
och connhandler.OutboundConnectionHandler
|
och connhandler.OutboundConnectionHandler
|
||||||
idh []*InboundDetourHandler
|
idh []*InboundDetourHandler
|
||||||
|
@ -9,7 +9,7 @@ func Int(value int) *IntSubject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IntSubject struct {
|
type IntSubject struct {
|
||||||
*Subject
|
Subject
|
||||||
value int
|
value int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -58,7 +58,7 @@ func socks5UDPRequest(address v2net.Address, payload []byte) []byte {
|
|||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
func setUpV2Ray() (uint16, error) {
|
func setUpV2Ray() (v2net.Port, error) {
|
||||||
id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
|
id1, err := config.NewID("ad937d9d-6e23-4a5a-ba23-bce5092a7c51")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Port uint16
|
Port v2net.Port
|
||||||
MsgProcessor func(msg []byte) []byte
|
MsgProcessor func(msg []byte) []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ func (server *Server) Start() (v2net.Address, error) {
|
|||||||
}
|
}
|
||||||
go server.acceptConnections(listener)
|
go server.acceptConnections(listener)
|
||||||
localAddr := listener.Addr().(*net.TCPAddr)
|
localAddr := listener.Addr().(*net.TCPAddr)
|
||||||
return v2net.IPAddress(localAddr.IP, uint16(localAddr.Port)), nil
|
return v2net.IPAddress(localAddr.IP, v2net.Port(localAddr.Port)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) acceptConnections(listener *net.TCPListener) {
|
func (server *Server) acceptConnections(listener *net.TCPListener) {
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
Port uint16
|
Port v2net.Port
|
||||||
MsgProcessor func(msg []byte) []byte
|
MsgProcessor func(msg []byte) []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -23,7 +23,7 @@ func (server *Server) Start() (v2net.Address, error) {
|
|||||||
}
|
}
|
||||||
go server.handleConnection(conn)
|
go server.handleConnection(conn)
|
||||||
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
localAddr := conn.LocalAddr().(*net.UDPAddr)
|
||||||
return v2net.IPAddress(localAddr.IP, uint16(localAddr.Port)), nil
|
return v2net.IPAddress(localAddr.IP, v2net.Port(localAddr.Port)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (server *Server) handleConnection(conn *net.UDPConn) {
|
func (server *Server) handleConnection(conn *net.UDPConn) {
|
||||||
|
Loading…
Reference in New Issue
Block a user