| // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | package v1 |
| 16 |
| 17 | import ( |
| 18 | "errors" |
| 19 | "fmt" |
| 20 | "reflect" |
| 21 |
| 22 | "github.com/fatedier/frp/pkg/util/jsonx" |
| 23 | ) |
| 24 |
| 25 | type DecodeOptions struct { |
| 26 | DisallowUnknownFields bool |
| 27 | } |
| 28 |
| 29 | func decodeJSONWithOptions(b []byte, out any, options DecodeOptions) error { |
| 30 | return jsonx.UnmarshalWithOptions(b, out, jsonx.DecodeOptions{ |
| 31 | RejectUnknownMembers: options.DisallowUnknownFields, |
| 32 | }) |
| 33 | } |
| 34 |
| 35 | func isJSONNull(b []byte) bool { |
| 36 | return len(b) == 0 || string(b) == "null" |
| 37 | } |
| 38 |
| 39 | type typedEnvelope struct { |
| 40 | Type string `json:"type"` |
| 41 | Plugin jsonx.RawMessage `json:"plugin,omitempty"` |
| 42 | } |
| 43 |
| 44 | func DecodeProxyConfigurerJSON(b []byte, options DecodeOptions) (ProxyConfigurer, error) { |
| 45 | if isJSONNull(b) { |
| 46 | return nil, errors.New("type is required") |
| 47 | } |
| 48 |
| 49 | var env typedEnvelope |
| 50 | if err := jsonx.Unmarshal(b, &env); err != nil { |
| 51 | return nil, err |
| 52 | } |
| 53 |
| 54 | configurer := NewProxyConfigurerByType(ProxyType(env.Type)) |
| 55 | if configurer == nil { |
| 56 | return nil, fmt.Errorf("unknown proxy type: %s", env.Type) |
| 57 | } |
| 58 | if err := decodeJSONWithOptions(b, configurer, options); err != nil { |
| 59 | return nil, fmt.Errorf("unmarshal ProxyConfig error: %v", err) |
| 60 | } |
| 61 |
| 62 | if len(env.Plugin) > 0 && !isJSONNull(env.Plugin) { |
| 63 | plugin, err := DecodeClientPluginOptionsJSON(env.Plugin, options) |
| 64 | if err != nil { |
| 65 | return nil, fmt.Errorf("unmarshal proxy plugin error: %v", err) |
| 66 | } |
| 67 | configurer.GetBaseConfig().Plugin = plugin |
| 68 | } |
| 69 | return configurer, nil |
| 70 | } |
| 71 |
| 72 | func DecodeVisitorConfigurerJSON(b []byte, options DecodeOptions) (VisitorConfigurer, error) { |
| 73 | if isJSONNull(b) { |
| 74 | return nil, errors.New("type is required") |
| 75 | } |
| 76 |
| 77 | var env typedEnvelope |
| 78 | if err := jsonx.Unmarshal(b, &env); err != nil { |
| 79 | return nil, err |
| 80 | } |
| 81 |
| 82 | configurer := NewVisitorConfigurerByType(VisitorType(env.Type)) |
| 83 | if configurer == nil { |
| 84 | return nil, fmt.Errorf("unknown visitor type: %s", env.Type) |
| 85 | } |
| 86 | if err := decodeJSONWithOptions(b, configurer, options); err != nil { |
| 87 | return nil, fmt.Errorf("unmarshal VisitorConfig error: %v", err) |
| 88 | } |
| 89 |
| 90 | if len(env.Plugin) > 0 && !isJSONNull(env.Plugin) { |
| 91 | plugin, err := DecodeVisitorPluginOptionsJSON(env.Plugin, options) |
| 92 | if err != nil { |
| 93 | return nil, fmt.Errorf("unmarshal visitor plugin error: %v", err) |
| 94 | } |
| 95 | configurer.GetBaseConfig().Plugin = plugin |
| 96 | } |
| 97 | return configurer, nil |
| 98 | } |
| 99 |
| 100 | func DecodeClientPluginOptionsJSON(b []byte, options DecodeOptions) (TypedClientPluginOptions, error) { |
| 101 | if isJSONNull(b) { |
| 102 | return TypedClientPluginOptions{}, nil |
| 103 | } |
| 104 |
| 105 | var env typedEnvelope |
| 106 | if err := jsonx.Unmarshal(b, &env); err != nil { |
| 107 | return TypedClientPluginOptions{}, err |
| 108 | } |
| 109 | if env.Type == "" { |
| 110 | return TypedClientPluginOptions{}, errors.New("plugin type is empty") |
| 111 | } |
| 112 |
| 113 | v, ok := clientPluginOptionsTypeMap[env.Type] |
| 114 | if !ok { |
| 115 | return TypedClientPluginOptions{}, fmt.Errorf("unknown plugin type: %s", env.Type) |
| 116 | } |
| 117 | optionsStruct := reflect.New(v).Interface().(ClientPluginOptions) |
| 118 | if err := decodeJSONWithOptions(b, optionsStruct, options); err != nil { |
| 119 | return TypedClientPluginOptions{}, fmt.Errorf("unmarshal ClientPluginOptions error: %v", err) |
| 120 | } |
| 121 | return TypedClientPluginOptions{ |
| 122 | Type: env.Type, |
| 123 | ClientPluginOptions: optionsStruct, |
| 124 | }, nil |
| 125 | } |
| 126 |
| 127 | func DecodeVisitorPluginOptionsJSON(b []byte, options DecodeOptions) (TypedVisitorPluginOptions, error) { |
| 128 | if isJSONNull(b) { |
| 129 | return TypedVisitorPluginOptions{}, nil |
| 130 | } |
| 131 |
| 132 | var env typedEnvelope |
| 133 | if err := jsonx.Unmarshal(b, &env); err != nil { |
| 134 | return TypedVisitorPluginOptions{}, err |
| 135 | } |
| 136 | if env.Type == "" { |
| 137 | return TypedVisitorPluginOptions{}, errors.New("visitor plugin type is empty") |
| 138 | } |
| 139 |
| 140 | v, ok := visitorPluginOptionsTypeMap[env.Type] |
| 141 | if !ok { |
| 142 | return TypedVisitorPluginOptions{}, fmt.Errorf("unknown visitor plugin type: %s", env.Type) |
| 143 | } |
| 144 | optionsStruct := reflect.New(v).Interface().(VisitorPluginOptions) |
| 145 | if err := decodeJSONWithOptions(b, optionsStruct, options); err != nil { |
| 146 | return TypedVisitorPluginOptions{}, fmt.Errorf("unmarshal VisitorPluginOptions error: %v", err) |
| 147 | } |
| 148 | return TypedVisitorPluginOptions{ |
| 149 | Type: env.Type, |
| 150 | VisitorPluginOptions: optionsStruct, |
| 151 | }, nil |
| 152 | } |
| 153 |
| 154 | func DecodeClientConfigJSON(b []byte, options DecodeOptions) (ClientConfig, error) { |
| 155 | type rawClientConfig struct { |
| 156 | ClientCommonConfig |
| 157 | Proxies []jsonx.RawMessage `json:"proxies,omitempty"` |
| 158 | Visitors []jsonx.RawMessage `json:"visitors,omitempty"` |
| 159 | } |
| 160 |
| 161 | raw := rawClientConfig{} |
| 162 | if err := decodeJSONWithOptions(b, &raw, options); err != nil { |
| 163 | return ClientConfig{}, err |
| 164 | } |
| 165 |
| 166 | cfg := ClientConfig{ |
| 167 | ClientCommonConfig: raw.ClientCommonConfig, |
| 168 | Proxies: make([]TypedProxyConfig, 0, len(raw.Proxies)), |
| 169 | Visitors: make([]TypedVisitorConfig, 0, len(raw.Visitors)), |
| 170 | } |
| 171 |
| 172 | for i, proxyData := range raw.Proxies { |
| 173 | proxyCfg, err := DecodeProxyConfigurerJSON(proxyData, options) |
| 174 | if err != nil { |
| 175 | return ClientConfig{}, fmt.Errorf("decode proxy at index %d: %w", i, err) |
| 176 | } |
| 177 | cfg.Proxies = append(cfg.Proxies, TypedProxyConfig{ |
| 178 | Type: proxyCfg.GetBaseConfig().Type, |
| 179 | ProxyConfigurer: proxyCfg, |
| 180 | }) |
| 181 | } |
| 182 |
| 183 | for i, visitorData := range raw.Visitors { |
| 184 | visitorCfg, err := DecodeVisitorConfigurerJSON(visitorData, options) |
| 185 | if err != nil { |
| 186 | return ClientConfig{}, fmt.Errorf("decode visitor at index %d: %w", i, err) |
| 187 | } |
| 188 | cfg.Visitors = append(cfg.Visitors, TypedVisitorConfig{ |
| 189 | Type: visitorCfg.GetBaseConfig().Type, |
| 190 | VisitorConfigurer: visitorCfg, |
| 191 | }) |
| 192 | } |
| 193 |
| 194 | return cfg, nil |
| 195 | } |