Skip to content

Commit

Permalink
Fix some parsing/serializing errors which are not corresponding to AP…
Browse files Browse the repository at this point in the history
…I specification

- 'credential' should be 'credentials' in vo::Ingress key
- 'auto' should be 'none' for the security value of VMESS adapter
- Fix shadowsocks examples
  • Loading branch information
Pichi committed Jul 23, 2021
1 parent 6e512d3 commit af3be7d
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 18 deletions.
4 changes: 2 additions & 2 deletions include/pichi/vo/keys.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ inline decltype(auto) LEAST_CONN = "least_conn";
namespace security {

inline decltype(auto) AUTO = "auto";
inline decltype(auto) NONE = "auto";
inline decltype(auto) NONE = "none";
inline decltype(auto) CHACHA20_IETF_POLY1305 = "chacha20-ietf-poly1305";
inline decltype(auto) AES_128_GCM = "aes-128-gcm";

Expand Down Expand Up @@ -119,7 +119,7 @@ inline decltype(auto) TYPE = "type";
inline decltype(auto) BIND = "bind";
inline decltype(auto) OPTION = "option";
inline decltype(auto) TLS = "tls";
inline decltype(auto) CREDENTIAL = "credential";
inline decltype(auto) CREDENTIALS = "credentials";
inline decltype(auto) WEBSOCKET = "websocket";

} // namespace ingress
Expand Down
2 changes: 1 addition & 1 deletion schemas/examples/egress/ss.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"host": "ss.example.com",
"port": 8388
},
"ss": {
"option": {
"password": "shadowsocks password",
"method": "rc4-md5"
}
Expand Down
2 changes: 1 addition & 1 deletion schemas/examples/ingress/ss.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"port": 8388
}
],
"ss": {
"option": {
"method": "xchacha20-ietf-poly1305",
"password": "shadowsocks password"
}
Expand Down
18 changes: 9 additions & 9 deletions src/vo/ingress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ json::Value toJson(Ingress const& ingress, Allocator& alloc)
case AdapterType::SOCKS5:
if (ingress.tls_.has_value()) ret.AddMember(ingress::TLS, toJson(*ingress.tls_, alloc), alloc);
if (ingress.credential_.has_value())
ret.AddMember(ingress::CREDENTIAL,
ret.AddMember(ingress::CREDENTIALS,
toJson(get<UpIngressCredential>(*ingress.credential_), alloc), alloc);
break;
case AdapterType::SS:
Expand All @@ -48,14 +48,14 @@ json::Value toJson(Ingress const& ingress, Allocator& alloc)
assertTrue(ingress.credential_.has_value());
ret.AddMember(ingress::OPTION, toJson(get<TrojanOption>(*ingress.opt_), alloc), alloc);
ret.AddMember(ingress::TLS, toJson(*ingress.tls_, alloc), alloc);
ret.AddMember(ingress::CREDENTIAL,
ret.AddMember(ingress::CREDENTIALS,
toJson(get<TrojanIngressCredential>(*ingress.credential_), alloc), alloc);
if (ingress.websocket_.has_value())
ret.AddMember(ingress::WEBSOCKET, toJson(*ingress.websocket_, alloc), alloc);
break;
case AdapterType::VMESS:
assertTrue(ingress.credential_.has_value());
ret.AddMember(ingress::CREDENTIAL,
ret.AddMember(ingress::CREDENTIALS,
toJson(get<VMessIngressCredential>(*ingress.credential_), alloc), alloc);
if (ingress.tls_.has_value()) ret.AddMember(ingress::TLS, toJson(*ingress.tls_, alloc), alloc);
if (ingress.websocket_.has_value())
Expand Down Expand Up @@ -93,26 +93,26 @@ template <> Ingress parse(json::Value const& v)
case AdapterType::HTTP:
case AdapterType::SOCKS5:
if (v.HasMember(ingress::TLS)) ingress.tls_ = parse<TlsIngressOption>(v[ingress::TLS]);
if (v.HasMember(ingress::CREDENTIAL))
ingress.credential_ = parse<UpIngressCredential>(v[ingress::CREDENTIAL]);
if (v.HasMember(ingress::CREDENTIALS))
ingress.credential_ = parse<UpIngressCredential>(v[ingress::CREDENTIALS]);
break;
case AdapterType::SS:
assertTrue(v.HasMember(ingress::OPTION), PichiError::BAD_JSON, msg::MISSING_OPTION_FIELD);
ingress.opt_ = parse<ShadowsocksOption>(v[ingress::OPTION]);
break;
case AdapterType::TROJAN:
assertTrue(v.HasMember(ingress::CREDENTIAL), PichiError::BAD_JSON, msg::MISSING_CRED_FIELD);
assertTrue(v.HasMember(ingress::CREDENTIALS), PichiError::BAD_JSON, msg::MISSING_CRED_FIELD);
assertTrue(v.HasMember(ingress::OPTION), PichiError::BAD_JSON, msg::MISSING_OPTION_FIELD);
assertTrue(v.HasMember(ingress::TLS), PichiError::BAD_JSON, msg::MISSING_TLS_FIELD);
ingress.credential_ = parse<TrojanIngressCredential>(v[ingress::CREDENTIAL]);
ingress.credential_ = parse<TrojanIngressCredential>(v[ingress::CREDENTIALS]);
ingress.opt_ = parse<TrojanOption>(v[ingress::OPTION]);
ingress.tls_ = parse<TlsIngressOption>(v[ingress::TLS]);
if (v.HasMember(ingress::WEBSOCKET))
ingress.websocket_ = parse<WebsocketOption>(v[ingress::WEBSOCKET]);
break;
case AdapterType::VMESS:
assertTrue(v.HasMember(ingress::CREDENTIAL), PichiError::BAD_JSON, msg::MISSING_CRED_FIELD);
ingress.credential_ = parse<VMessIngressCredential>(v[ingress::CREDENTIAL]);
assertTrue(v.HasMember(ingress::CREDENTIALS), PichiError::BAD_JSON, msg::MISSING_CRED_FIELD);
ingress.credential_ = parse<VMessIngressCredential>(v[ingress::CREDENTIALS]);
if (v.HasMember(ingress::TLS)) ingress.tls_ = parse<TlsIngressOption>(v[ingress::TLS]);
if (v.HasMember(ingress::WEBSOCKET))
ingress.websocket_ = parse<WebsocketOption>(v[ingress::WEBSOCKET]);
Expand Down
11 changes: 6 additions & 5 deletions test/vo_ingress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ template <AdapterType type> Value defaultIngressJson()
ingress.AddMember(ingress::BIND, Value{kArrayType}, alloc);
ingress[ingress::BIND].PushBack(toJson(DEFAULT_ENDPOINT, alloc), alloc);
if constexpr (Trait::credential_ == Present::MANDATORY)
ingress.AddMember(ingress::CREDENTIAL, defaultCredentialJson<typename Trait::Credential>(),
ingress.AddMember(ingress::CREDENTIALS, defaultCredentialJson<typename Trait::Credential>(),
alloc);
if constexpr (Trait::option_ == Present::MANDATORY)
ingress.AddMember(ingress::OPTION, defaultOptionJson<typename Trait::Option>(), alloc);
Expand Down Expand Up @@ -188,7 +188,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(parse_Empty_Bind, Trait, AllAdapterTraits)
BOOST_AUTO_TEST_CASE_TEMPLATE(parse_Mandatory_Fields, Trait, AllAdapterTraits)
{
if constexpr (Trait::credential_ == Present::MANDATORY)
verifyMandatoryField<Trait::type_>(ingress::CREDENTIAL);
verifyMandatoryField<Trait::type_>(ingress::CREDENTIALS);
if constexpr (Trait::option_ == Present::MANDATORY)
verifyMandatoryField<Trait::type_>(ingress::OPTION);
if constexpr (Trait::tls_ == Present::MANDATORY) verifyMandatoryField<Trait::type_>(ingress::TLS);
Expand All @@ -203,7 +203,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(parse_Optional_Fields, Trait, AllAdapterTraits)
auto ingress = defaultIngress<Trait::type_>();
ingress.credential_ = defaultCredential<Credential>();
BOOST_CHECK(parse<Ingress>(defaultIngressJson<Trait::type_>().AddMember(
ingress::CREDENTIAL, defaultCredentialJson<Credential>(), alloc)) == ingress);
ingress::CREDENTIALS, defaultCredentialJson<Credential>(), alloc)) == ingress);
}
if constexpr (Trait::option_ == Present::OPTIONAL) {
using Option = typename Trait::Option;
Expand All @@ -230,7 +230,7 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(parse_Unused_Fields, Trait, AllAdapterTraits)
{
auto json = defaultIngressJson<Trait::type_>();
if constexpr (Trait::credential_ == Present::UNUSED)
json.AddMember(ingress::CREDENTIAL, Value{kObjectType}, alloc);
json.AddMember(ingress::CREDENTIALS, Value{kObjectType}, alloc);
if constexpr (Trait::option_ == Present::UNUSED)
json.AddMember(ingress::OPTION, Value{kObjectType}, alloc);
if constexpr (Trait::tls_ == Present::UNUSED)
Expand Down Expand Up @@ -293,7 +293,8 @@ BOOST_AUTO_TEST_CASE_TEMPLATE(toJson_Optional_Fields, Trait, AllAdapterTraits)

if constexpr (Trait::credential_ == Present::OPTIONAL) {
ingress.credential_ = defaultCredential<typename Trait::Credential>();
json.AddMember(ingress::CREDENTIAL, defaultCredentialJson<typename Trait::Credential>(), alloc);
json.AddMember(ingress::CREDENTIALS, defaultCredentialJson<typename Trait::Credential>(),
alloc);
}
if constexpr (Trait::option_ == Present::OPTIONAL) {
ingress.opt_ = defaultOption<typename Trait::Option>();
Expand Down

0 comments on commit af3be7d

Please sign in to comment.