mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-17 23:06:30 -05:00
Move socks config into a sparate folder
This commit is contained in:
parent
3747e45978
commit
13e595e4cb
@ -1,22 +0,0 @@
|
|||||||
package socks
|
|
||||||
|
|
||||||
import (
|
|
||||||
"encoding/json"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
JsonAuthMethodNoAuth = "noauth"
|
|
||||||
JsonAuthMethodUserPass = "password"
|
|
||||||
)
|
|
||||||
|
|
||||||
type SocksConfig struct {
|
|
||||||
AuthMethod string `json:"auth"`
|
|
||||||
Username string `json:"user"`
|
|
||||||
Password string `json:"pass"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadConfig(rawConfig []byte) (SocksConfig, error) {
|
|
||||||
config := SocksConfig{}
|
|
||||||
err := json.Unmarshal(rawConfig, &config)
|
|
||||||
return config, err
|
|
||||||
}
|
|
30
proxy/socks/config/json/config.go
Normal file
30
proxy/socks/config/json/config.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package json
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
AuthMethodNoAuth = "noauth"
|
||||||
|
AuthMethodUserPass = "password"
|
||||||
|
)
|
||||||
|
|
||||||
|
type SocksConfig struct {
|
||||||
|
AuthMethod string `json:"auth"`
|
||||||
|
Username string `json:"user"`
|
||||||
|
Password string `json:"pass"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SocksConfig) IsNoAuth() bool {
|
||||||
|
return config.AuthMethod == AuthMethodNoAuth
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config SocksConfig) IsPassword() bool {
|
||||||
|
return config.AuthMethod == AuthMethodUserPass
|
||||||
|
}
|
||||||
|
|
||||||
|
func Load(rawConfig []byte) (SocksConfig, error) {
|
||||||
|
config := SocksConfig{}
|
||||||
|
err := json.Unmarshal(rawConfig, &config)
|
||||||
|
return config, err
|
||||||
|
}
|
@ -10,6 +10,7 @@ import (
|
|||||||
"github.com/v2ray/v2ray-core/common/errors"
|
"github.com/v2ray/v2ray-core/common/errors"
|
||||||
"github.com/v2ray/v2ray-core/common/log"
|
"github.com/v2ray/v2ray-core/common/log"
|
||||||
v2net "github.com/v2ray/v2ray-core/common/net"
|
v2net "github.com/v2ray/v2ray-core/common/net"
|
||||||
|
jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||||
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
"github.com/v2ray/v2ray-core/proxy/socks/protocol"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -17,11 +18,11 @@ import (
|
|||||||
type SocksServer struct {
|
type SocksServer struct {
|
||||||
accepting bool
|
accepting bool
|
||||||
vPoint *core.Point
|
vPoint *core.Point
|
||||||
config SocksConfig
|
config jsonconfig.SocksConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer {
|
func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer {
|
||||||
config, err := loadConfig(rawConfig)
|
config, err := jsonconfig.Load(rawConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Unable to load socks config: %v", err)
|
log.Error("Unable to load socks config: %v", err)
|
||||||
panic(errors.NewConfigurationError())
|
panic(errors.NewConfigurationError())
|
||||||
@ -83,7 +84,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
dest = v2net.NewTCPDestination(v2net.IPAddress(auth4.IP[:], auth4.Port))
|
dest = v2net.NewTCPDestination(v2net.IPAddress(auth4.IP[:], auth4.Port))
|
||||||
} else {
|
} else {
|
||||||
expectedAuthMethod := protocol.AuthNotRequired
|
expectedAuthMethod := protocol.AuthNotRequired
|
||||||
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
if server.config.IsPassword() {
|
||||||
expectedAuthMethod = protocol.AuthUserPass
|
expectedAuthMethod = protocol.AuthUserPass
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +105,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error {
|
|||||||
log.Error("Socks failed to write authentication: %v", err)
|
log.Error("Socks failed to write authentication: %v", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if server.config.AuthMethod == JsonAuthMethodUserPass {
|
if server.config.IsPassword() {
|
||||||
upRequest, err := protocol.ReadUserPassRequest(reader)
|
upRequest, err := protocol.ReadUserPassRequest(reader)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error("Socks failed to read username and password: %v", err)
|
log.Error("Socks failed to read username and password: %v", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user