config.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package simplified
  2. import (
  3. "context"
  4. "encoding/json"
  5. "github.com/golang/protobuf/jsonpb"
  6. "github.com/v2fly/v2ray-core/v5/common"
  7. "github.com/v2fly/v2ray-core/v5/common/protocol"
  8. "github.com/v2fly/v2ray-core/v5/common/serial"
  9. "github.com/v2fly/v2ray-core/v5/proxy/shadowsocks"
  10. )
  11. func (c *CipherTypeWrapper) UnmarshalJSONPB(unmarshaler *jsonpb.Unmarshaler, bytes []byte) error {
  12. var method string
  13. if err := json.Unmarshal(bytes, &method); err != nil {
  14. return err
  15. }
  16. if c.Value = shadowsocks.CipherFromString(method); c.Value == shadowsocks.CipherType_UNKNOWN {
  17. return newError("unknown cipher method: ", method)
  18. }
  19. return nil
  20. }
  21. func (c *CipherTypeWrapper) MarshalJSONPB(marshaler *jsonpb.Marshaler) ([]byte, error) {
  22. method := c.Value.String()
  23. return json.Marshal(method)
  24. }
  25. func init() {
  26. common.Must(common.RegisterConfig((*ServerConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  27. simplifiedServer := config.(*ServerConfig)
  28. fullServer := &shadowsocks.ServerConfig{
  29. User: &protocol.User{
  30. Account: serial.ToTypedMessage(&shadowsocks.Account{
  31. Password: simplifiedServer.Password,
  32. CipherType: simplifiedServer.Method.Value,
  33. }),
  34. },
  35. Network: simplifiedServer.Networks.GetNetwork(),
  36. PacketEncoding: simplifiedServer.PacketEncoding,
  37. }
  38. return common.CreateObject(ctx, fullServer)
  39. }))
  40. common.Must(common.RegisterConfig((*ClientConfig)(nil), func(ctx context.Context, config interface{}) (interface{}, error) {
  41. simplifiedClient := config.(*ClientConfig)
  42. fullClient := &shadowsocks.ClientConfig{
  43. Server: []*protocol.ServerEndpoint{
  44. {
  45. Address: simplifiedClient.Address,
  46. Port: simplifiedClient.Port,
  47. User: []*protocol.User{
  48. {
  49. Account: serial.ToTypedMessage(&shadowsocks.Account{
  50. Password: simplifiedClient.Password,
  51. CipherType: simplifiedClient.Method.Value,
  52. ExperimentReducedIvHeadEntropy: simplifiedClient.ExperimentReducedIvHeadEntropy,
  53. }),
  54. },
  55. },
  56. },
  57. },
  58. }
  59. return common.CreateObject(ctx, fullClient)
  60. }))
  61. }