mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-21 17:46:58 -05:00
add interface for socks config
This commit is contained in:
parent
73807390f1
commit
f69b83f3e6
13
proxy/socks/config/config.go
Normal file
13
proxy/socks/config/config.go
Normal file
@ -0,0 +1,13 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"net"
|
||||
)
|
||||
|
||||
type SocksConfig interface {
|
||||
IsNoAuth() bool
|
||||
IsPassword() bool
|
||||
HasAccount(username, password string) bool
|
||||
IP() net.IP
|
||||
UDPEnabled() bool
|
||||
}
|
@ -59,7 +59,7 @@ func (this *IPAddress) UnmarshalJSON(data []byte) error {
|
||||
type SocksConfig struct {
|
||||
AuthMethod string `json:"auth"`
|
||||
Accounts SocksAccountMap `json:"accounts"`
|
||||
UDPEnabled bool `json:"udp"`
|
||||
UDP bool `json:"udp"`
|
||||
HostIP IPAddress `json:"ip"`
|
||||
}
|
||||
|
||||
@ -79,6 +79,10 @@ func (sc *SocksConfig) IP() net.IP {
|
||||
return net.IP(sc.HostIP)
|
||||
}
|
||||
|
||||
func (this *SocksConfig) UDPEnabled() bool {
|
||||
return this.UDP
|
||||
}
|
||||
|
||||
func init() {
|
||||
jsonconfig.RegisterInboundConnectionConfig("socks", func() interface{} {
|
||||
return &SocksConfig{
|
||||
|
@ -49,7 +49,7 @@ func TestNoAuthConfig(t *testing.T) {
|
||||
assert.Bool(config.IsNoAuth()).IsTrue()
|
||||
assert.Bool(config.IsPassword()).IsFalse()
|
||||
assert.String(config.IP()).Equals("8.8.8.8")
|
||||
assert.Bool(config.UDPEnabled).IsFalse()
|
||||
assert.Bool(config.UDPEnabled()).IsFalse()
|
||||
}
|
||||
|
||||
func TestUserPassConfig(t *testing.T) {
|
||||
@ -61,5 +61,5 @@ func TestUserPassConfig(t *testing.T) {
|
||||
assert.Bool(config.IsNoAuth()).IsFalse()
|
||||
assert.Bool(config.IsPassword()).IsTrue()
|
||||
assert.Bool(config.HasAccount("x", "y")).IsTrue()
|
||||
assert.Bool(config.UDPEnabled).IsTrue()
|
||||
assert.Bool(config.UDPEnabled()).IsTrue()
|
||||
}
|
||||
|
@ -13,7 +13,7 @@ import (
|
||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||
"github.com/v2ray/v2ray-core/common/retry"
|
||||
proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors"
|
||||
jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/config"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
||||
)
|
||||
|
||||
@ -26,10 +26,10 @@ var (
|
||||
type SocksServer struct {
|
||||
accepting bool
|
||||
dispatcher app.PacketDispatcher
|
||||
config *jsonconfig.SocksConfig
|
||||
config config.SocksConfig
|
||||
}
|
||||
|
||||
func NewSocksServer(dispatcher app.PacketDispatcher, config *jsonconfig.SocksConfig) *SocksServer {
|
||||
func NewSocksServer(dispatcher app.PacketDispatcher, config config.SocksConfig) *SocksServer {
|
||||
return &SocksServer{
|
||||
dispatcher: dispatcher,
|
||||
config: config,
|
||||
@ -48,7 +48,7 @@ func (this *SocksServer) Listen(port v2net.Port) error {
|
||||
}
|
||||
this.accepting = true
|
||||
go this.AcceptConnections(listener)
|
||||
if this.config.UDPEnabled {
|
||||
if this.config.UDPEnabled() {
|
||||
this.ListenUDP(port)
|
||||
}
|
||||
return nil
|
||||
@ -138,7 +138,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri
|
||||
return err
|
||||
}
|
||||
|
||||
if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled {
|
||||
if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled() {
|
||||
return this.handleUDP(reader, writer)
|
||||
}
|
||||
|
||||
|
@ -239,7 +239,7 @@ func TestSocksUdpSend(t *testing.T) {
|
||||
ProtocolValue: "socks",
|
||||
SettingsValue: &json.SocksConfig{
|
||||
AuthMethod: "noauth",
|
||||
UDPEnabled: true,
|
||||
UDP: true,
|
||||
},
|
||||
},
|
||||
OutboundConfigValue: &mocks.ConnectionConfig{
|
||||
|
@ -3,14 +3,14 @@ package socks
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core/app"
|
||||
"github.com/v2ray/v2ray-core/proxy/common/connhandler"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/config"
|
||||
)
|
||||
|
||||
type SocksServerFactory struct {
|
||||
}
|
||||
|
||||
func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) {
|
||||
return NewSocksServer(dispatcher, rawConfig.(*json.SocksConfig)), nil
|
||||
return NewSocksServer(dispatcher, rawConfig.(config.SocksConfig)), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
@ -22,6 +22,7 @@ import (
|
||||
_ "github.com/v2ray/v2ray-core/proxy/freedom"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/freedom/config/json"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/socks"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/vmess"
|
||||
_ "github.com/v2ray/v2ray-core/proxy/vmess/config/json"
|
||||
)
|
||||
|
@ -106,7 +106,7 @@ func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, e
|
||||
ProtocolValue: "socks",
|
||||
SettingsValue: &socksjson.SocksConfig{
|
||||
AuthMethod: "noauth",
|
||||
UDPEnabled: true,
|
||||
UDP: true,
|
||||
HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
|
||||
},
|
||||
},
|
||||
@ -131,7 +131,7 @@ func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, e
|
||||
ProtocolValue: "socks",
|
||||
SettingsValue: &socksjson.SocksConfig{
|
||||
AuthMethod: "noauth",
|
||||
UDPEnabled: false,
|
||||
UDP: false,
|
||||
HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)),
|
||||
},
|
||||
},
|
||||
|
@ -41,7 +41,7 @@ func TestBuildAndRun(t *testing.T) {
|
||||
errStr := string(errBuffer.Bytes())
|
||||
|
||||
assert.Bool(strings.Contains(outStr, "v1.0")).IsTrue()
|
||||
assert.Int(len(errStr)).Equals(0)
|
||||
assert.StringLiteral(errStr).Equals("")
|
||||
|
||||
os.Remove(target)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user