Skip to content

Commit

Permalink
refine shadowsocks proto
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Sep 25, 2016
1 parent 5e90177 commit ce5bc72
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 82 deletions.
12 changes: 6 additions & 6 deletions proxy/shadowsocks/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
"v2ray.com/core/common/protocol"
)

func (this *Config) GetCipher() Cipher {
switch this.Cipher {
case Config_AES_128_CFB:
func (this *Account) GetCipher() Cipher {
switch this.CipherType {
case CipherType_AES_128_CFB:
return &AesCfb{KeyBytes: 16}
case Config_AES_256_CFB:
case CipherType_AES_256_CFB:
return &AesCfb{KeyBytes: 32}
case Config_CHACHA20:
case CipherType_CHACHA20:
return &ChaCha20{IVBytes: 8}
case Config_CHACHA20_IEFT:
case CipherType_CHACHA20_IEFT:
return &ChaCha20{IVBytes: 12}
}
panic("Failed to create Cipher. Should not happen.")
Expand Down
112 changes: 67 additions & 45 deletions proxy/shadowsocks/config.pb.go

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

32 changes: 19 additions & 13 deletions proxy/shadowsocks/config.proto
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
syntax = "proto3";

import "v2ray.com/core/common/protocol/user.proto";

package com.v2ray.core.proxy.shadowsocks;
option go_package = "shadowsocks";

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

message Account {
string password = 1;
CipherType cipher_type = 2;
}

enum CipherType {
UNKNOWN = 0;
AES_128_CFB = 1;
AES_256_CFB = 2;
CHACHA20 = 3;
CHACHA20_IEFT = 4;
}

message ServerConfig {
bool udp_enabled = 1;
com.v2ray.core.common.protocol.User user = 2;
}

message Config {
enum Cipher {
UNKNOWN = 0;
AES_128_CFB = 1;
AES_256_CFB = 2;
CHACHA20 = 3;
CHACHA20_IEFT = 4;
}
Cipher cipher = 1;
bool udp_enabled = 2;
com.v2ray.core.common.protocol.User user = 3;
message ClientConfig {
repeated com.v2ray.core.common.protocol.ServerSpecPB server = 1;
}
30 changes: 16 additions & 14 deletions proxy/shadowsocks/config_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/golang/protobuf/ptypes"
)

func (this *Config) UnmarshalJSON(data []byte) error {
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Cipher string `json:"method"`
Password string `json:"password"`
Expand All @@ -29,41 +29,43 @@ func (this *Config) UnmarshalJSON(data []byte) error {
}

this.UdpEnabled = jsonConfig.UDP

if len(jsonConfig.Password) == 0 {
log.Error("Shadowsocks: Password is not specified.")
return common.ErrBadConfiguration
}
account := &Account{
Password: jsonConfig.Password,
}
jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
switch jsonConfig.Cipher {
case "aes-256-cfb":
this.Cipher = Config_AES_256_CFB
account.CipherType = CipherType_AES_256_CFB
case "aes-128-cfb":
this.Cipher = Config_AES_128_CFB
account.CipherType = CipherType_AES_128_CFB
case "chacha20":
this.Cipher = Config_CHACHA20
account.CipherType = CipherType_CHACHA20
case "chacha20-ietf":
this.Cipher = Config_CHACHA20_IEFT
account.CipherType = CipherType_CHACHA20_IEFT
default:
log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
return common.ErrBadConfiguration
}

if len(jsonConfig.Password) == 0 {
log.Error("Shadowsocks: Password is not specified.")
return common.ErrBadConfiguration
}
account, err := ptypes.MarshalAny(&Account{
Password: jsonConfig.Password,
})
anyAccount, err := ptypes.MarshalAny(account)
if err != nil {
log.Error("Shadowsocks: Failed to create account: ", err)
return common.ErrBadConfiguration
}
this.User = &protocol.User{
Email: jsonConfig.Email,
Level: uint32(jsonConfig.Level),
Account: account,
Account: anyAccount,
}

return nil
}

func init() {
registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(Config) })
registry.RegisterInboundConfig("shadowsocks", func() interface{} { return new(ServerConfig) })
}
8 changes: 4 additions & 4 deletions proxy/shadowsocks/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (

type Server struct {
packetDispatcher dispatcher.PacketDispatcher
config *Config
config *ServerConfig
cipher Cipher
cipherKey []byte
meta *proxy.InboundHandlerMeta
Expand All @@ -33,15 +33,15 @@ type Server struct {
udpServer *udp.UDPServer
}

func NewServer(config *Config, space app.Space, meta *proxy.InboundHandlerMeta) (*Server, error) {
func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandlerMeta) (*Server, error) {
if config.GetUser() == nil {
return nil, protocol.ErrUserMissing
}
account := new(Account)
if _, err := config.GetUser().GetTypedAccount(account); err != nil {
return nil, err
}
cipher := config.GetCipher()
cipher := account.GetCipher()
s := &Server{
config: config,
meta: meta,
Expand Down Expand Up @@ -283,7 +283,7 @@ func (this *ServerFactory) Create(space app.Space, rawConfig interface{}, meta *
if !space.HasApp(dispatcher.APP_ID) {
return nil, common.ErrBadConfiguration
}
return NewServer(rawConfig.(*Config), space, meta)
return NewServer(rawConfig.(*ServerConfig), space, meta)
}

func init() {
Expand Down

0 comments on commit ce5bc72

Please sign in to comment.