Skip to content

Commit

Permalink
refine socks config
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Sep 25, 2016
1 parent c6a7389 commit c5f09c6
Show file tree
Hide file tree
Showing 12 changed files with 214 additions and 251 deletions.
20 changes: 0 additions & 20 deletions proxy/socks/account.go

This file was deleted.

28 changes: 0 additions & 28 deletions proxy/socks/client_config.go

This file was deleted.

61 changes: 0 additions & 61 deletions proxy/socks/client_config.pb.go

This file was deleted.

9 changes: 0 additions & 9 deletions proxy/socks/client_config.proto

This file was deleted.

47 changes: 43 additions & 4 deletions proxy/socks/server_config_json.go → proxy/socks/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// +build json

package socks

import (
Expand All @@ -9,9 +7,50 @@ import (
"v2ray.com/core/common"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/registry"

"github.com/golang/protobuf/ptypes"
google_protobuf "github.com/golang/protobuf/ptypes/any"
)

func (this *Account) Equals(another protocol.Account) bool {
if account, ok := another.(*Account); ok {
return this.Username == account.Username
}
return false
}

func (this *Account) AsAccount() (protocol.Account, error) {
return this, nil
}

func NewAccount() protocol.AsAccount {
return &Account{}
}

func (this *Account) AsAny() (*google_protobuf.Any, error) {
return ptypes.MarshalAny(this)
}

func (this *ServerConfig) HasAccount(username, password string) bool {
if this.Accounts == nil {
return false
}
storedPassed, found := this.Accounts[username]
if !found {
return false
}
return storedPassed == password
}

func (this *ServerConfig) GetNetAddress() v2net.Address {
if this.Address == nil {
return v2net.LocalHostIP
}
return this.Address.AsAddress()
}

const (
AuthMethodNoAuth = "noauth"
AuthMethodUserPass = "password"
Expand All @@ -31,9 +70,9 @@ func (this *ServerConfig) UnmarshalJSON(data []byte) error {
return errors.New("Socks: Failed to parse config: " + err.Error())
}
if rawConfig.AuthMethod == AuthMethodNoAuth {
this.AuthType = ServerConfig_NO_AUTH
this.AuthType = AuthType_NO_AUTH
} else if rawConfig.AuthMethod == AuthMethodUserPass {
this.AuthType = ServerConfig_PASSWORD
this.AuthType = AuthType_PASSWORD
} else {
log.Error("Socks: Unknown auth method: ", rawConfig.AuthMethod)
return common.ErrBadConfiguration
Expand Down
147 changes: 147 additions & 0 deletions proxy/socks/config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions proxy/socks/server_config.proto → proxy/socks/config.proto
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@ package com.v2ray.core.proxy.socks;
option go_package = "socks";

import "v2ray.com/core/common/net/address.proto";
import "v2ray.com/core/common/protocol/server_spec.proto";

message Account {
string username = 1;
string password = 2;
}

enum AuthType {
NO_AUTH = 0;
PASSWORD = 1;
}

message ServerConfig {
enum AuthType {
NO_AUTH = 0;
PASSWORD = 1;
}
AuthType auth_type = 1;
map<string, string> accounts = 2;
com.v2ray.core.common.net.AddressPB address = 3;
bool udp_enabled = 4;
uint32 timeout = 5;
}

message ClientConfig {
repeated com.v2ray.core.common.protocol.ServerSpecPB server = 1;
}
11 changes: 7 additions & 4 deletions proxy/socks/client_config_json.go → proxy/socks/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ func (this *ClientConfig) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, jsonConfig); err != nil {
return errors.New("Socks|Client: Failed to parse config: " + err.Error())
}
this.Servers = make([]*protocol.ServerSpec, len(jsonConfig.Servers))
this.Server = make([]*protocol.ServerSpecPB, len(jsonConfig.Servers))
for idx, serverConfig := range jsonConfig.Servers {
server := protocol.NewServerSpec(NewAccount, v2net.TCPDestination(serverConfig.Address.AsAddress(), serverConfig.Port), protocol.AlwaysValid())
server := &protocol.ServerSpecPB{
Address: serverConfig.Address,
Port: uint32(serverConfig.Port),
}
for _, rawUser := range serverConfig.Users {
user := new(protocol.User)
if err := json.Unmarshal(rawUser, user); err != nil {
Expand All @@ -55,9 +58,9 @@ func (this *ClientConfig) UnmarshalJSON(data []byte) error {
return err
}
user.Account = anyAccount
server.AddUser(user)
server.Users = append(server.Users, user)
}
this.Servers[idx] = server
this.Server[idx] = server
}
return nil
}
Expand Down
File renamed without changes.
Loading

0 comments on commit c5f09c6

Please sign in to comment.