forked from yanue/V2rayU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSample.swift
165 lines (154 loc) · 3.27 KB
/
Sample.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//
// Sample.swift
// V2rayUTests
//
// Created by yanue on 2018/10/26.
// Copyright © 2018 yanue. All rights reserved.
//
import Foundation
let jsonTxt = """
{
"inbound": {
"port": "1080",
"listen": "localhost",
"protocol": "http",
"tab": "aaaa",
"settings": {
"timeout": 0,
"accounts": [
{
"user": "my-username",
"pass": "my-password"
}
],
"allowTransparent": false,
"userLevel": 0
}
},
"inboundDetour": [
{
"port": 1080,
"listen": "127.0.0.1",
"protocol": "socks",
"settings": {
"auth": "noauth",
"timeout": 360,
"udp": true
}
}
],
"outbound": {
"tag": "agentout",
"protocol": "vmess",
"streamSettings": {
"network": "h2",
"httpSettings": {
"host": [
"host"
],
"path": "/ssl"
},
"tlsSettings": {},
"security": "tls"
},
"settings": {
"vnext": [
{
"users": [
{
"alterId": 64,
"id": "uuid"
}
],
"port": 443,
"address": "host"
}
]
}
},
"outboundDetour": [
{
"tag": "direct",
"protocol": "freedom",
"settings": {
"response": null
}
},
{
"tag": "blockout",
"protocol": "blackhole",
"settings": {
"response": {
"type": "http"
}
}
}
],
"routing": {
"strategy": "rules",
"settings": {
"domainStrategy": "IPIfNonMatch",
"rules": [
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:private"
]
},
{
"type": "field",
"outboundTag": "direct",
"domain": [
"geosite:cn"
]
},
{
"type": "field",
"outboundTag": "direct",
"ip": [
"geoip:cn"
]
}
]
}
}
}
"""
struct PersonProfile1: Codable {
var desc: String = "aaa"
}
struct PersonProfile2: Codable {
var intro: String = "aaa"
}
struct Person: Codable {
var name: String = "bbb"
var profile1: PersonProfile1?
var profile2: PersonProfile2?
enum CodingKeys: String, CodingKey {
case name = "title"
case setting
}
}
extension Person {
init(from decoder: Decoder) throws {
let vals = try decoder.container(keyedBy: CodingKeys.self)
name = try vals.decode(String.self, forKey: CodingKeys.name)
if name == "aa" {
try vals.decode(PersonProfile1.self, forKey: .setting)
} else {
try vals.decode(PersonProfile2.self, forKey: .setting)
}
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(name, forKey: .name)
// var profile = container.nestedContainer(keyedBy: CodingKeys.self, forKey: .setting)
// try profile.encode(self.profile, forKey: .setting)
if name == "aa" {
try container.encode(self.profile1, forKey: .setting)
} else {
try container.encode(self.profile2, forKey: .setting)
}
}
}