1
0

conversion.go 12 KB

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