diff --git a/pkg/config/v1/proxy.go b/pkg/config/v1/proxy.go index ec3379ec..1449f46a 100644 --- a/pkg/config/v1/proxy.go +++ b/pkg/config/v1/proxy.go @@ -189,6 +189,10 @@ func (c *TypedProxyConfig) UnmarshalJSON(b []byte) error { return nil } +func (c *TypedProxyConfig) MarshalJSON() ([]byte, error) { + return json.Marshal(c.ProxyConfigurer) +} + type ProxyConfigurer interface { Complete(namePrefix string) GetBaseConfig() *ProxyBaseConfig diff --git a/pkg/config/v1/proxy_test.go b/pkg/config/v1/proxy_test.go index 64753250..8f311859 100644 --- a/pkg/config/v1/proxy_test.go +++ b/pkg/config/v1/proxy_test.go @@ -19,6 +19,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "sigs.k8s.io/yaml" ) func TestUnmarshalTypedProxyConfig(t *testing.T) { @@ -47,3 +48,58 @@ func TestUnmarshalTypedProxyConfig(t *testing.T) { require.IsType(&TCPProxyConfig{}, proxyConfigs.Proxies[0].ProxyConfigurer) require.IsType(&HTTPProxyConfig{}, proxyConfigs.Proxies[1].ProxyConfigurer) } + +func TestMarshalTypedProxyConfig(t *testing.T) { + require := require.New(t) + + clientConfig := ClientConfig{ + ClientCommonConfig: ClientCommonConfig{ + Auth: AuthClientConfig{ + Method: AuthMethodToken, + Token: "update-me", + }, + ServerAddr: "frp.example.org", + ServerPort: 8080, + Log: LogConfig{ + Level: "info", + }, + }, + Proxies: []TypedProxyConfig{ + { + Type: string(ProxyTypeTCP), + ProxyConfigurer: &TCPProxyConfig{ + ProxyBaseConfig: ProxyBaseConfig{ + Name: "proxy1", + Type: string(ProxyTypeTCP), + ProxyBackend: ProxyBackend{ + LocalIP: "192.168.0.101", + LocalPort: 8889, + }, + }, + RemotePort: 30001, + }, + }, + { + Type: string(ProxyTypeTCP), + ProxyConfigurer: &TCPProxyConfig{ + ProxyBaseConfig: ProxyBaseConfig{ + Name: "proxy2", + Type: string(ProxyTypeTCP), + ProxyBackend: ProxyBackend{ + LocalIP: "192.168.0.102", + LocalPort: 8889, + }, + }, + RemotePort: 30002, + }, + }, + }, + } + + yamlConfig, err := yaml.Marshal(&clientConfig) + require.NoError(err) + clientConfigUnmarshalled := ClientConfig{} + err = yaml.Unmarshal(yamlConfig, &clientConfigUnmarshalled) + require.NoError(err) + require.Equal(clientConfig, clientConfigUnmarshalled) +}