conversion.go 12 KB

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