| // |
| 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/fatedier/frp/pkg/util/jsonx" |
| 21 | "github.com/fatedier/frp/pkg/util/util" |
| 22 | ) |
| 23 |
| 24 | type VisitorTransport struct { |
| 25 | UseEncryption bool `json:"useEncryption,omitempty"` |
| 26 | UseCompression bool `json:"useCompression,omitempty"` |
| 27 | } |
| 28 |
| 29 | type VisitorBaseConfig struct { |
| 30 | Name string `json:"name"` |
| 31 | Type string `json:"type"` |
| 32 | // Enabled controls whether this visitor is enabled. nil or true means enabled, false means disabled. |
| 33 | // This allows individual control over each visitor, complementing the global "start" field. |
| 34 | Enabled *bool `json:"enabled,omitempty"` |
| 35 | Transport VisitorTransport `json:"transport,omitempty"` |
| 36 | SecretKey string `json:"secretKey,omitempty"` |
| 37 | // if the server user is not set, it defaults to the current user |
| 38 | ServerUser string `json:"serverUser,omitempty"` |
| 39 | ServerName string `json:"serverName,omitempty"` |
| 40 | BindAddr string `json:"bindAddr,omitempty"` |
| 41 | // BindPort is the port that visitor listens on. |
| 42 | // It can be less than 0, it means don't bind to the port and only receive connections redirected from |
| 43 | // other visitors. (This is not supported for SUDP now) |
| 44 | BindPort int `json:"bindPort,omitempty"` |
| 45 |
| 46 | // Plugin specifies what plugin should be used. |
| 47 | Plugin TypedVisitorPluginOptions `json:"plugin,omitempty"` |
| 48 | } |
| 49 |
| 50 | func (c VisitorBaseConfig) Clone() VisitorBaseConfig { |
| 51 | out := c |
| 52 | out.Enabled = util.ClonePtr(c.Enabled) |
| 53 | out.Plugin = c.Plugin.Clone() |
| 54 | return out |
| 55 | } |
| 56 |
| 57 | func (c *VisitorBaseConfig) GetBaseConfig() *VisitorBaseConfig { |
| 58 | return c |
| 59 | } |
| 60 |
| 61 | func (c *VisitorBaseConfig) Complete() { |
| 62 | if c.BindAddr == "" { |
| 63 | c.BindAddr = "127.0.0.1" |
| 64 | } |
| 65 | } |
| 66 |
| 67 | type VisitorConfigurer interface { |
| 68 | Complete() |
| 69 | GetBaseConfig() *VisitorBaseConfig |
| 70 | Clone() VisitorConfigurer |
| 71 | } |
| 72 |
| 73 | type VisitorType string |
| 74 |
| 75 | const ( |
| 76 | VisitorTypeSTCP VisitorType = "stcp" |
| 77 | VisitorTypeXTCP VisitorType = "xtcp" |
| 78 | VisitorTypeSUDP VisitorType = "sudp" |
| 79 | ) |
| 80 |
| 81 | var visitorConfigTypeMap = map[VisitorType]reflect.Type{ |
| 82 | VisitorTypeSTCP: reflect.TypeFor[STCPVisitorConfig](), |
| 83 | VisitorTypeXTCP: reflect.TypeFor[XTCPVisitorConfig](), |
| 84 | VisitorTypeSUDP: reflect.TypeFor[SUDPVisitorConfig](), |
| 85 | } |
| 86 |
| 87 | type TypedVisitorConfig struct { |
| 88 | Type string `json:"type"` |
| 89 | VisitorConfigurer |
| 90 | } |
| 91 |
| 92 | func (c *TypedVisitorConfig) UnmarshalJSON(b []byte) error { |
| 93 | configurer, err := DecodeVisitorConfigurerJSON(b, DecodeOptions{}) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 |
| 98 | c.Type = configurer.GetBaseConfig().Type |
| 99 | c.VisitorConfigurer = configurer |
| 100 | return nil |
| 101 | } |
| 102 |
| 103 | func (c *TypedVisitorConfig) MarshalJSON() ([]byte, error) { |
| 104 | return jsonx.Marshal(c.VisitorConfigurer) |
| 105 | } |
| 106 |
| 107 | func NewVisitorConfigurerByType(t VisitorType) VisitorConfigurer { |
| 108 | v, ok := visitorConfigTypeMap[t] |
| 109 | if !ok { |
| 110 | return nil |
| 111 | } |
| 112 | vc := reflect.New(v).Interface().(VisitorConfigurer) |
| 113 | vc.GetBaseConfig().Type = string(t) |
| 114 | return vc |
| 115 | } |
| 116 |
| 117 | var _ VisitorConfigurer = &STCPVisitorConfig{} |
| 118 |
| 119 | type STCPVisitorConfig struct { |
| 120 | VisitorBaseConfig |
| 121 | } |
| 122 |
| 123 | func (c *STCPVisitorConfig) Clone() VisitorConfigurer { |
| 124 | out := *c |
| 125 | out.VisitorBaseConfig = c.VisitorBaseConfig.Clone() |
| 126 | return &out |
| 127 | } |
| 128 |
| 129 | var _ VisitorConfigurer = &SUDPVisitorConfig{} |
| 130 |
| 131 | type SUDPVisitorConfig struct { |
| 132 | VisitorBaseConfig |
| 133 | } |
| 134 |
| 135 | func (c *SUDPVisitorConfig) Clone() VisitorConfigurer { |
| 136 | out := *c |
| 137 | out.VisitorBaseConfig = c.VisitorBaseConfig.Clone() |
| 138 | return &out |
| 139 | } |
| 140 |
| 141 | var _ VisitorConfigurer = &XTCPVisitorConfig{} |
| 142 |
| 143 | type XTCPVisitorConfig struct { |
| 144 | VisitorBaseConfig |
| 145 |
| 146 | Protocol string `json:"protocol,omitempty"` |
| 147 | KeepTunnelOpen bool `json:"keepTunnelOpen,omitempty"` |
| 148 | MaxRetriesAnHour int `json:"maxRetriesAnHour,omitempty"` |
| 149 | MinRetryInterval int `json:"minRetryInterval,omitempty"` |
| 150 | FallbackTo string `json:"fallbackTo,omitempty"` |
| 151 | FallbackTimeoutMs int `json:"fallbackTimeoutMs,omitempty"` |
| 152 |
| 153 | // NatTraversal configuration for NAT traversal |
| 154 | NatTraversal *NatTraversalConfig `json:"natTraversal,omitempty"` |
| 155 | } |
| 156 |
| 157 | func (c *XTCPVisitorConfig) Complete() { |
| 158 | c.VisitorBaseConfig.Complete() |
| 159 |
| 160 | c.Protocol = util.EmptyOr(c.Protocol, "quic") |
| 161 | c.MaxRetriesAnHour = util.EmptyOr(c.MaxRetriesAnHour, 8) |
| 162 | c.MinRetryInterval = util.EmptyOr(c.MinRetryInterval, 90) |
| 163 | c.FallbackTimeoutMs = util.EmptyOr(c.FallbackTimeoutMs, 1000) |
| 164 | } |
| 165 |
| 166 | func (c *XTCPVisitorConfig) Clone() VisitorConfigurer { |
| 167 | out := *c |
| 168 | out.VisitorBaseConfig = c.VisitorBaseConfig.Clone() |
| 169 | out.NatTraversal = c.NatTraversal.Clone() |
| 170 | return &out |
| 171 | } |