| // |
| 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 | "reflect" |
| 19 |
| 20 | "github.com/samber/lo" |
| 21 |
| 22 | "github.com/fatedier/frp/pkg/util/jsonx" |
| 23 | "github.com/fatedier/frp/pkg/util/util" |
| 24 | ) |
| 25 |
| 26 | const ( |
| 27 | PluginHTTP2HTTPS = "http2https" |
| 28 | PluginHTTPProxy = "http_proxy" |
| 29 | PluginHTTPS2HTTP = "https2http" |
| 30 | PluginHTTPS2HTTPS = "https2https" |
| 31 | PluginHTTP2HTTP = "http2http" |
| 32 | PluginSocks5 = "socks5" |
| 33 | PluginStaticFile = "static_file" |
| 34 | PluginUnixDomainSocket = "unix_domain_socket" |
| 35 | PluginTLS2Raw = "tls2raw" |
| 36 | PluginVirtualNet = "virtual_net" |
| 37 | ) |
| 38 |
| 39 | var clientPluginOptionsTypeMap = map[string]reflect.Type{ |
| 40 | PluginHTTP2HTTPS: reflect.TypeFor[HTTP2HTTPSPluginOptions](), |
| 41 | PluginHTTPProxy: reflect.TypeFor[HTTPProxyPluginOptions](), |
| 42 | PluginHTTPS2HTTP: reflect.TypeFor[HTTPS2HTTPPluginOptions](), |
| 43 | PluginHTTPS2HTTPS: reflect.TypeFor[HTTPS2HTTPSPluginOptions](), |
| 44 | PluginHTTP2HTTP: reflect.TypeFor[HTTP2HTTPPluginOptions](), |
| 45 | PluginSocks5: reflect.TypeFor[Socks5PluginOptions](), |
| 46 | PluginStaticFile: reflect.TypeFor[StaticFilePluginOptions](), |
| 47 | PluginUnixDomainSocket: reflect.TypeFor[UnixDomainSocketPluginOptions](), |
| 48 | PluginTLS2Raw: reflect.TypeFor[TLS2RawPluginOptions](), |
| 49 | PluginVirtualNet: reflect.TypeFor[VirtualNetPluginOptions](), |
| 50 | } |
| 51 |
| 52 | type ClientPluginOptions interface { |
| 53 | Complete() |
| 54 | Clone() ClientPluginOptions |
| 55 | } |
| 56 |
| 57 | type TypedClientPluginOptions struct { |
| 58 | Type string `json:"type"` |
| 59 | ClientPluginOptions |
| 60 | } |
| 61 |
| 62 | func (c TypedClientPluginOptions) Clone() TypedClientPluginOptions { |
| 63 | out := c |
| 64 | if c.ClientPluginOptions != nil { |
| 65 | out.ClientPluginOptions = c.ClientPluginOptions.Clone() |
| 66 | } |
| 67 | return out |
| 68 | } |
| 69 |
| 70 | func (c *TypedClientPluginOptions) UnmarshalJSON(b []byte) error { |
| 71 | decoded, err := DecodeClientPluginOptionsJSON(b, DecodeOptions{}) |
| 72 | if err != nil { |
| 73 | return err |
| 74 | } |
| 75 | *c = decoded |
| 76 | return nil |
| 77 | } |
| 78 |
| 79 | func (c *TypedClientPluginOptions) MarshalJSON() ([]byte, error) { |
| 80 | return jsonx.Marshal(c.ClientPluginOptions) |
| 81 | } |
| 82 |
| 83 | type HTTP2HTTPSPluginOptions struct { |
| 84 | Type string `json:"type,omitempty"` |
| 85 | LocalAddr string `json:"localAddr,omitempty"` |
| 86 | HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"` |
| 87 | RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"` |
| 88 | } |
| 89 |
| 90 | func (o *HTTP2HTTPSPluginOptions) Complete() {} |
| 91 |
| 92 | func (o *HTTP2HTTPSPluginOptions) Clone() ClientPluginOptions { |
| 93 | if o == nil { |
| 94 | return nil |
| 95 | } |
| 96 | out := *o |
| 97 | out.RequestHeaders = o.RequestHeaders.Clone() |
| 98 | return &out |
| 99 | } |
| 100 |
| 101 | type HTTPProxyPluginOptions struct { |
| 102 | Type string `json:"type,omitempty"` |
| 103 | HTTPUser string `json:"httpUser,omitempty"` |
| 104 | HTTPPassword string `json:"httpPassword,omitempty"` |
| 105 | } |
| 106 |
| 107 | func (o *HTTPProxyPluginOptions) Complete() {} |
| 108 |
| 109 | func (o *HTTPProxyPluginOptions) Clone() ClientPluginOptions { |
| 110 | if o == nil { |
| 111 | return nil |
| 112 | } |
| 113 | out := *o |
| 114 | return &out |
| 115 | } |
| 116 |
| 117 | type HTTPS2HTTPPluginOptions struct { |
| 118 | Type string `json:"type,omitempty"` |
| 119 | LocalAddr string `json:"localAddr,omitempty"` |
| 120 | HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"` |
| 121 | RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"` |
| 122 | EnableHTTP2 *bool `json:"enableHTTP2,omitempty"` |
| 123 | CrtPath string `json:"crtPath,omitempty"` |
| 124 | KeyPath string `json:"keyPath,omitempty"` |
| 125 | } |
| 126 |
| 127 | func (o *HTTPS2HTTPPluginOptions) Complete() { |
| 128 | o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true)) |
| 129 | } |
| 130 |
| 131 | func (o *HTTPS2HTTPPluginOptions) Clone() ClientPluginOptions { |
| 132 | if o == nil { |
| 133 | return nil |
| 134 | } |
| 135 | out := *o |
| 136 | out.RequestHeaders = o.RequestHeaders.Clone() |
| 137 | out.EnableHTTP2 = util.ClonePtr(o.EnableHTTP2) |
| 138 | return &out |
| 139 | } |
| 140 |
| 141 | type HTTPS2HTTPSPluginOptions struct { |
| 142 | Type string `json:"type,omitempty"` |
| 143 | LocalAddr string `json:"localAddr,omitempty"` |
| 144 | HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"` |
| 145 | RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"` |
| 146 | EnableHTTP2 *bool `json:"enableHTTP2,omitempty"` |
| 147 | CrtPath string `json:"crtPath,omitempty"` |
| 148 | KeyPath string `json:"keyPath,omitempty"` |
| 149 | } |
| 150 |
| 151 | func (o *HTTPS2HTTPSPluginOptions) Complete() { |
| 152 | o.EnableHTTP2 = util.EmptyOr(o.EnableHTTP2, lo.ToPtr(true)) |
| 153 | } |
| 154 |
| 155 | func (o *HTTPS2HTTPSPluginOptions) Clone() ClientPluginOptions { |
| 156 | if o == nil { |
| 157 | return nil |
| 158 | } |
| 159 | out := *o |
| 160 | out.RequestHeaders = o.RequestHeaders.Clone() |
| 161 | out.EnableHTTP2 = util.ClonePtr(o.EnableHTTP2) |
| 162 | return &out |
| 163 | } |
| 164 |
| 165 | type HTTP2HTTPPluginOptions struct { |
| 166 | Type string `json:"type,omitempty"` |
| 167 | LocalAddr string `json:"localAddr,omitempty"` |
| 168 | HostHeaderRewrite string `json:"hostHeaderRewrite,omitempty"` |
| 169 | RequestHeaders HeaderOperations `json:"requestHeaders,omitempty"` |
| 170 | } |
| 171 |
| 172 | func (o *HTTP2HTTPPluginOptions) Complete() {} |
| 173 |
| 174 | func (o *HTTP2HTTPPluginOptions) Clone() ClientPluginOptions { |
| 175 | if o == nil { |
| 176 | return nil |
| 177 | } |
| 178 | out := *o |
| 179 | out.RequestHeaders = o.RequestHeaders.Clone() |
| 180 | return &out |
| 181 | } |
| 182 |
| 183 | type Socks5PluginOptions struct { |
| 184 | Type string `json:"type,omitempty"` |
| 185 | Username string `json:"username,omitempty"` |
| 186 | Password string `json:"password,omitempty"` |
| 187 | } |
| 188 |
| 189 | func (o *Socks5PluginOptions) Complete() {} |
| 190 |
| 191 | func (o *Socks5PluginOptions) Clone() ClientPluginOptions { |
| 192 | if o == nil { |
| 193 | return nil |
| 194 | } |
| 195 | out := *o |
| 196 | return &out |
| 197 | } |
| 198 |
| 199 | type StaticFilePluginOptions struct { |
| 200 | Type string `json:"type,omitempty"` |
| 201 | LocalPath string `json:"localPath,omitempty"` |
| 202 | StripPrefix string `json:"stripPrefix,omitempty"` |
| 203 | HTTPUser string `json:"httpUser,omitempty"` |
| 204 | HTTPPassword string `json:"httpPassword,omitempty"` |
| 205 | } |
| 206 |
| 207 | func (o *StaticFilePluginOptions) Complete() {} |
| 208 |
| 209 | func (o *StaticFilePluginOptions) Clone() ClientPluginOptions { |
| 210 | if o == nil { |
| 211 | return nil |
| 212 | } |
| 213 | out := *o |
| 214 | return &out |
| 215 | } |
| 216 |
| 217 | type UnixDomainSocketPluginOptions struct { |
| 218 | Type string `json:"type,omitempty"` |
| 219 | UnixPath string `json:"unixPath,omitempty"` |
| 220 | } |
| 221 |
| 222 | func (o *UnixDomainSocketPluginOptions) Complete() {} |
| 223 |
| 224 | func (o *UnixDomainSocketPluginOptions) Clone() ClientPluginOptions { |
| 225 | if o == nil { |
| 226 | return nil |
| 227 | } |
| 228 | out := *o |
| 229 | return &out |
| 230 | } |
| 231 |
| 232 | type TLS2RawPluginOptions struct { |
| 233 | Type string `json:"type,omitempty"` |
| 234 | LocalAddr string `json:"localAddr,omitempty"` |
| 235 | CrtPath string `json:"crtPath,omitempty"` |
| 236 | KeyPath string `json:"keyPath,omitempty"` |
| 237 | } |
| 238 |
| 239 | func (o *TLS2RawPluginOptions) Complete() {} |
| 240 |
| 241 | func (o *TLS2RawPluginOptions) Clone() ClientPluginOptions { |
| 242 | if o == nil { |
| 243 | return nil |
| 244 | } |
| 245 | out := *o |
| 246 | return &out |
| 247 | } |
| 248 |
| 249 | type VirtualNetPluginOptions struct { |
| 250 | Type string `json:"type,omitempty"` |
| 251 | } |
| 252 |
| 253 | func (o *VirtualNetPluginOptions) Complete() {} |
| 254 |
| 255 | func (o *VirtualNetPluginOptions) Clone() ClientPluginOptions { |
| 256 | if o == nil { |
| 257 | return nil |
| 258 | } |
| 259 | out := *o |
| 260 | return &out |
| 261 | } |