| // |
| 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 legacy |
| 16 |
| 17 | import ( |
| 18 | "strings" |
| 19 |
| 20 | "github.com/samber/lo" |
| 21 |
| 22 | "github.com/fatedier/frp/pkg/config/types" |
| 23 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 24 | ) |
| 25 |
| 26 | func Convert_ClientCommonConf_To_v1(conf *ClientCommonConf) *v1.ClientCommonConfig { |
| 27 | out := &v1.ClientCommonConfig{} |
| 28 | out.User = conf.User |
| 29 | out.Auth.Method = v1.AuthMethod(conf.AuthenticationMethod) |
| 30 | out.Auth.Token = conf.Token |
| 31 | if conf.AuthenticateHeartBeats { |
| 32 | out.Auth.AdditionalScopes = append(out.Auth.AdditionalScopes, v1.AuthScopeHeartBeats) |
| 33 | } |
| 34 | if conf.AuthenticateNewWorkConns { |
| 35 | out.Auth.AdditionalScopes = append(out.Auth.AdditionalScopes, v1.AuthScopeNewWorkConns) |
| 36 | } |
| 37 | out.Auth.OIDC.ClientID = conf.OidcClientID |
| 38 | out.Auth.OIDC.ClientSecret = conf.OidcClientSecret |
| 39 | out.Auth.OIDC.Audience = conf.OidcAudience |
| 40 | out.Auth.OIDC.Scope = conf.OidcScope |
| 41 | out.Auth.OIDC.TokenEndpointURL = conf.OidcTokenEndpointURL |
| 42 | out.Auth.OIDC.AdditionalEndpointParams = conf.OidcAdditionalEndpointParams |
| 43 |
| 44 | out.ServerAddr = conf.ServerAddr |
| 45 | out.ServerPort = conf.ServerPort |
| 46 | out.NatHoleSTUNServer = conf.NatHoleSTUNServer |
| 47 | out.Transport.DialServerTimeout = conf.DialServerTimeout |
| 48 | out.Transport.DialServerKeepAlive = conf.DialServerKeepAlive |
| 49 | out.Transport.ConnectServerLocalIP = conf.ConnectServerLocalIP |
| 50 | out.Transport.ProxyURL = conf.HTTPProxy |
| 51 | out.Transport.PoolCount = conf.PoolCount |
| 52 | out.Transport.TCPMux = lo.ToPtr(conf.TCPMux) |
| 53 | out.Transport.TCPMuxKeepaliveInterval = conf.TCPMuxKeepaliveInterval |
| 54 | out.Transport.Protocol = conf.Protocol |
| 55 | out.Transport.HeartbeatInterval = conf.HeartbeatInterval |
| 56 | out.Transport.HeartbeatTimeout = conf.HeartbeatTimeout |
| 57 | out.Transport.QUIC.KeepalivePeriod = conf.QUICKeepalivePeriod |
| 58 | out.Transport.QUIC.MaxIdleTimeout = conf.QUICMaxIdleTimeout |
| 59 | out.Transport.QUIC.MaxIncomingStreams = conf.QUICMaxIncomingStreams |
| 60 | out.Transport.TLS.Enable = lo.ToPtr(conf.TLSEnable) |
| 61 | out.Transport.TLS.DisableCustomTLSFirstByte = lo.ToPtr(conf.DisableCustomTLSFirstByte) |
| 62 | out.Transport.TLS.CertFile = conf.TLSCertFile |
| 63 | out.Transport.TLS.KeyFile = conf.TLSKeyFile |
| 64 | out.Transport.TLS.TrustedCaFile = conf.TLSTrustedCaFile |
| 65 | out.Transport.TLS.ServerName = conf.TLSServerName |
| 66 |
| 67 | out.Log.To = conf.LogFile |
| 68 | out.Log.Level = conf.LogLevel |
| 69 | out.Log.MaxDays = conf.LogMaxDays |
| 70 | out.Log.DisablePrintColor = conf.DisableLogColor |
| 71 |
| 72 | out.WebServer.Addr = conf.AdminAddr |
| 73 | out.WebServer.Port = conf.AdminPort |
| 74 | out.WebServer.User = conf.AdminUser |
| 75 | out.WebServer.Password = conf.AdminPwd |
| 76 | out.WebServer.AssetsDir = conf.AssetsDir |
| 77 | out.WebServer.PprofEnable = conf.PprofEnable |
| 78 |
| 79 | out.DNSServer = conf.DNSServer |
| 80 | out.LoginFailExit = lo.ToPtr(conf.LoginFailExit) |
| 81 | out.Start = conf.Start |
| 82 | out.UDPPacketSize = conf.UDPPacketSize |
| 83 | out.Metadatas = conf.Metas |
| 84 | out.IncludeConfigFiles = conf.IncludeConfigFiles |
| 85 | return out |
| 86 | } |
| 87 |
| 88 | func Convert_ServerCommonConf_To_v1(conf *ServerCommonConf) *v1.ServerConfig { |
| 89 | out := &v1.ServerConfig{} |
| 90 | out.Auth.Method = v1.AuthMethod(conf.AuthenticationMethod) |
| 91 | out.Auth.Token = conf.Token |
| 92 | if conf.AuthenticateHeartBeats { |
| 93 | out.Auth.AdditionalScopes = append(out.Auth.AdditionalScopes, v1.AuthScopeHeartBeats) |
| 94 | } |
| 95 | if conf.AuthenticateNewWorkConns { |
| 96 | out.Auth.AdditionalScopes = append(out.Auth.AdditionalScopes, v1.AuthScopeNewWorkConns) |
| 97 | } |
| 98 | out.Auth.OIDC.Audience = conf.OidcAudience |
| 99 | out.Auth.OIDC.Issuer = conf.OidcIssuer |
| 100 | out.Auth.OIDC.SkipExpiryCheck = conf.OidcSkipExpiryCheck |
| 101 | out.Auth.OIDC.SkipIssuerCheck = conf.OidcSkipIssuerCheck |
| 102 |
| 103 | out.BindAddr = conf.BindAddr |
| 104 | out.BindPort = conf.BindPort |
| 105 | out.KCPBindPort = conf.KCPBindPort |
| 106 | out.QUICBindPort = conf.QUICBindPort |
| 107 | out.Transport.QUIC.KeepalivePeriod = conf.QUICKeepalivePeriod |
| 108 | out.Transport.QUIC.MaxIdleTimeout = conf.QUICMaxIdleTimeout |
| 109 | out.Transport.QUIC.MaxIncomingStreams = conf.QUICMaxIncomingStreams |
| 110 |
| 111 | out.ProxyBindAddr = conf.ProxyBindAddr |
| 112 | out.VhostHTTPPort = conf.VhostHTTPPort |
| 113 | out.VhostHTTPSPort = conf.VhostHTTPSPort |
| 114 | out.TCPMuxHTTPConnectPort = conf.TCPMuxHTTPConnectPort |
| 115 | out.TCPMuxPassthrough = conf.TCPMuxPassthrough |
| 116 | out.VhostHTTPTimeout = conf.VhostHTTPTimeout |
| 117 |
| 118 | out.WebServer.Addr = conf.DashboardAddr |
| 119 | out.WebServer.Port = conf.DashboardPort |
| 120 | out.WebServer.User = conf.DashboardUser |
| 121 | out.WebServer.Password = conf.DashboardPwd |
| 122 | out.WebServer.AssetsDir = conf.AssetsDir |
| 123 | if conf.DashboardTLSMode { |
| 124 | out.WebServer.TLS = &v1.TLSConfig{} |
| 125 | out.WebServer.TLS.CertFile = conf.DashboardTLSCertFile |
| 126 | out.WebServer.TLS.KeyFile = conf.DashboardTLSKeyFile |
| 127 | out.WebServer.PprofEnable = conf.PprofEnable |
| 128 | } |
| 129 |
| 130 | out.EnablePrometheus = conf.EnablePrometheus |
| 131 |
| 132 | out.Log.To = conf.LogFile |
| 133 | out.Log.Level = conf.LogLevel |
| 134 | out.Log.MaxDays = conf.LogMaxDays |
| 135 | out.Log.DisablePrintColor = conf.DisableLogColor |
| 136 |
| 137 | out.DetailedErrorsToClient = lo.ToPtr(conf.DetailedErrorsToClient) |
| 138 | out.SubDomainHost = conf.SubDomainHost |
| 139 | out.Custom404Page = conf.Custom404Page |
| 140 | out.UserConnTimeout = conf.UserConnTimeout |
| 141 | out.UDPPacketSize = conf.UDPPacketSize |
| 142 | out.NatHoleAnalysisDataReserveHours = conf.NatHoleAnalysisDataReserveHours |
| 143 |
| 144 | out.Transport.TCPMux = lo.ToPtr(conf.TCPMux) |
| 145 | out.Transport.TCPMuxKeepaliveInterval = conf.TCPMuxKeepaliveInterval |
| 146 | out.Transport.TCPKeepAlive = conf.TCPKeepAlive |
| 147 | out.Transport.MaxPoolCount = conf.MaxPoolCount |
| 148 | out.Transport.HeartbeatTimeout = conf.HeartbeatTimeout |
| 149 |
| 150 | out.Transport.TLS.Force = conf.TLSOnly |
| 151 | out.Transport.TLS.CertFile = conf.TLSCertFile |
| 152 | out.Transport.TLS.KeyFile = conf.TLSKeyFile |
| 153 | out.Transport.TLS.TrustedCaFile = conf.TLSTrustedCaFile |
| 154 |
| 155 | out.MaxPortsPerClient = conf.MaxPortsPerClient |
| 156 |
| 157 | for _, v := range conf.HTTPPlugins { |
| 158 | out.HTTPPlugins = append(out.HTTPPlugins, v1.HTTPPluginOptions{ |
| 159 | Name: v.Name, |
| 160 | Addr: v.Addr, |
| 161 | Path: v.Path, |
| 162 | Ops: v.Ops, |
| 163 | TLSVerify: v.TLSVerify, |
| 164 | }) |
| 165 | } |
| 166 |
| 167 | out.AllowPorts, _ = types.NewPortsRangeSliceFromString(conf.AllowPortsStr) |
| 168 | return out |
| 169 | } |
| 170 |
| 171 | func transformHeadersFromPluginParams(params map[string]string) v1.HeaderOperations { |
| 172 | out := v1.HeaderOperations{} |
| 173 | for k, v := range params { |
| 174 | k, ok := strings.CutPrefix(k, "plugin_header_") |
| 175 | if !ok || k == "" { |
| 176 | continue |
| 177 | } |
| 178 | if out.Set == nil { |
| 179 | out.Set = make(map[string]string) |
| 180 | } |
| 181 | out.Set[k] = v |
| 182 | } |
| 183 | return out |
| 184 | } |
| 185 |
| 186 | func Convert_ProxyConf_To_v1_Base(conf ProxyConf) *v1.ProxyBaseConfig { |
| 187 | out := &v1.ProxyBaseConfig{} |
| 188 | base := conf.GetBaseConfig() |
| 189 |
| 190 | out.Name = base.ProxyName |
| 191 | out.Type = base.ProxyType |
| 192 | out.Metadatas = base.Metas |
| 193 |
| 194 | out.Transport.UseEncryption = base.UseEncryption |
| 195 | out.Transport.UseCompression = base.UseCompression |
| 196 | out.Transport.BandwidthLimit = base.BandwidthLimit |
| 197 | out.Transport.BandwidthLimitMode = base.BandwidthLimitMode |
| 198 | out.Transport.ProxyProtocolVersion = base.ProxyProtocolVersion |
| 199 |
| 200 | out.LoadBalancer.Group = base.Group |
| 201 | out.LoadBalancer.GroupKey = base.GroupKey |
| 202 |
| 203 | out.HealthCheck.Type = base.HealthCheckType |
| 204 | out.HealthCheck.TimeoutSeconds = base.HealthCheckTimeoutS |
| 205 | out.HealthCheck.MaxFailed = base.HealthCheckMaxFailed |
| 206 | out.HealthCheck.IntervalSeconds = base.HealthCheckIntervalS |
| 207 | out.HealthCheck.Path = base.HealthCheckURL |
| 208 |
| 209 | out.LocalIP = base.LocalIP |
| 210 | out.LocalPort = base.LocalPort |
| 211 |
| 212 | switch base.Plugin { |
| 213 | case "http2https": |
| 214 | out.Plugin.ClientPluginOptions = &v1.HTTP2HTTPSPluginOptions{ |
| 215 | LocalAddr: base.PluginParams["plugin_local_addr"], |
| 216 | HostHeaderRewrite: base.PluginParams["plugin_host_header_rewrite"], |
| 217 | RequestHeaders: transformHeadersFromPluginParams(base.PluginParams), |
| 218 | } |
| 219 | case "http_proxy": |
| 220 | out.Plugin.ClientPluginOptions = &v1.HTTPProxyPluginOptions{ |
| 221 | HTTPUser: base.PluginParams["plugin_http_user"], |
| 222 | HTTPPassword: base.PluginParams["plugin_http_passwd"], |
| 223 | } |
| 224 | case "https2http": |
| 225 | out.Plugin.ClientPluginOptions = &v1.HTTPS2HTTPPluginOptions{ |
| 226 | LocalAddr: base.PluginParams["plugin_local_addr"], |
| 227 | HostHeaderRewrite: base.PluginParams["plugin_host_header_rewrite"], |
| 228 | RequestHeaders: transformHeadersFromPluginParams(base.PluginParams), |
| 229 | CrtPath: base.PluginParams["plugin_crt_path"], |
| 230 | KeyPath: base.PluginParams["plugin_key_path"], |
| 231 | } |
| 232 | case "https2https": |
| 233 | out.Plugin.ClientPluginOptions = &v1.HTTPS2HTTPSPluginOptions{ |
| 234 | LocalAddr: base.PluginParams["plugin_local_addr"], |
| 235 | HostHeaderRewrite: base.PluginParams["plugin_host_header_rewrite"], |
| 236 | RequestHeaders: transformHeadersFromPluginParams(base.PluginParams), |
| 237 | CrtPath: base.PluginParams["plugin_crt_path"], |
| 238 | KeyPath: base.PluginParams["plugin_key_path"], |
| 239 | } |
| 240 | case "socks5": |
| 241 | out.Plugin.ClientPluginOptions = &v1.Socks5PluginOptions{ |
| 242 | Username: base.PluginParams["plugin_user"], |
| 243 | Password: base.PluginParams["plugin_passwd"], |
| 244 | } |
| 245 | case "static_file": |
| 246 | out.Plugin.ClientPluginOptions = &v1.StaticFilePluginOptions{ |
| 247 | LocalPath: base.PluginParams["plugin_local_path"], |
| 248 | StripPrefix: base.PluginParams["plugin_strip_prefix"], |
| 249 | HTTPUser: base.PluginParams["plugin_http_user"], |
| 250 | HTTPPassword: base.PluginParams["plugin_http_passwd"], |
| 251 | } |
| 252 | case "unix_domain_socket": |
| 253 | out.Plugin.ClientPluginOptions = &v1.UnixDomainSocketPluginOptions{ |
| 254 | UnixPath: base.PluginParams["plugin_unix_path"], |
| 255 | } |
| 256 | } |
| 257 | out.Plugin.Type = base.Plugin |
| 258 | return out |
| 259 | } |
| 260 |
| 261 | func Convert_ProxyConf_To_v1(conf ProxyConf) v1.ProxyConfigurer { |
| 262 | outBase := Convert_ProxyConf_To_v1_Base(conf) |
| 263 | var out v1.ProxyConfigurer |
| 264 | switch v := conf.(type) { |
| 265 | case *TCPProxyConf: |
| 266 | c := &v1.TCPProxyConfig{ProxyBaseConfig: *outBase} |
| 267 | c.RemotePort = v.RemotePort |
| 268 | out = c |
| 269 | case *UDPProxyConf: |
| 270 | c := &v1.UDPProxyConfig{ProxyBaseConfig: *outBase} |
| 271 | c.RemotePort = v.RemotePort |
| 272 | out = c |
| 273 | case *HTTPProxyConf: |
| 274 | c := &v1.HTTPProxyConfig{ProxyBaseConfig: *outBase} |
| 275 | c.CustomDomains = v.CustomDomains |
| 276 | c.SubDomain = v.SubDomain |
| 277 | c.Locations = v.Locations |
| 278 | c.HTTPUser = v.HTTPUser |
| 279 | c.HTTPPassword = v.HTTPPwd |
| 280 | c.HostHeaderRewrite = v.HostHeaderRewrite |
| 281 | c.RequestHeaders.Set = v.Headers |
| 282 | c.RouteByHTTPUser = v.RouteByHTTPUser |
| 283 | out = c |
| 284 | case *HTTPSProxyConf: |
| 285 | c := &v1.HTTPSProxyConfig{ProxyBaseConfig: *outBase} |
| 286 | c.CustomDomains = v.CustomDomains |
| 287 | c.SubDomain = v.SubDomain |
| 288 | out = c |
| 289 | case *TCPMuxProxyConf: |
| 290 | c := &v1.TCPMuxProxyConfig{ProxyBaseConfig: *outBase} |
| 291 | c.CustomDomains = v.CustomDomains |
| 292 | c.SubDomain = v.SubDomain |
| 293 | c.HTTPUser = v.HTTPUser |
| 294 | c.HTTPPassword = v.HTTPPwd |
| 295 | c.RouteByHTTPUser = v.RouteByHTTPUser |
| 296 | c.Multiplexer = v.Multiplexer |
| 297 | out = c |
| 298 | case *STCPProxyConf: |
| 299 | c := &v1.STCPProxyConfig{ProxyBaseConfig: *outBase} |
| 300 | c.Secretkey = v.Sk |
| 301 | c.AllowUsers = v.AllowUsers |
| 302 | out = c |
| 303 | case *SUDPProxyConf: |
| 304 | c := &v1.SUDPProxyConfig{ProxyBaseConfig: *outBase} |
| 305 | c.Secretkey = v.Sk |
| 306 | c.AllowUsers = v.AllowUsers |
| 307 | out = c |
| 308 | case *XTCPProxyConf: |
| 309 | c := &v1.XTCPProxyConfig{ProxyBaseConfig: *outBase} |
| 310 | c.Secretkey = v.Sk |
| 311 | c.AllowUsers = v.AllowUsers |
| 312 | out = c |
| 313 | } |
| 314 | return out |
| 315 | } |
| 316 |
| 317 | func Convert_VisitorConf_To_v1_Base(conf VisitorConf) *v1.VisitorBaseConfig { |
| 318 | out := &v1.VisitorBaseConfig{} |
| 319 | base := conf.GetBaseConfig() |
| 320 |
| 321 | out.Name = base.ProxyName |
| 322 | out.Type = base.ProxyType |
| 323 | out.Transport.UseEncryption = base.UseEncryption |
| 324 | out.Transport.UseCompression = base.UseCompression |
| 325 | out.SecretKey = base.Sk |
| 326 | out.ServerUser = base.ServerUser |
| 327 | out.ServerName = base.ServerName |
| 328 | out.BindAddr = base.BindAddr |
| 329 | out.BindPort = base.BindPort |
| 330 | return out |
| 331 | } |
| 332 |
| 333 | func Convert_VisitorConf_To_v1(conf VisitorConf) v1.VisitorConfigurer { |
| 334 | outBase := Convert_VisitorConf_To_v1_Base(conf) |
| 335 | var out v1.VisitorConfigurer |
| 336 | switch v := conf.(type) { |
| 337 | case *STCPVisitorConf: |
| 338 | c := &v1.STCPVisitorConfig{VisitorBaseConfig: *outBase} |
| 339 | out = c |
| 340 | case *SUDPVisitorConf: |
| 341 | c := &v1.SUDPVisitorConfig{VisitorBaseConfig: *outBase} |
| 342 | out = c |
| 343 | case *XTCPVisitorConf: |
| 344 | c := &v1.XTCPVisitorConfig{VisitorBaseConfig: *outBase} |
| 345 | c.Protocol = v.Protocol |
| 346 | c.KeepTunnelOpen = v.KeepTunnelOpen |
| 347 | c.MaxRetriesAnHour = v.MaxRetriesAnHour |
| 348 | c.MinRetryInterval = v.MinRetryInterval |
| 349 | c.FallbackTo = v.FallbackTo |
| 350 | c.FallbackTimeoutMs = v.FallbackTimeoutMs |
| 351 | out = c |
| 352 | } |
| 353 | return out |
| 354 | } |