fix: ClientConfig marshals incorrectly

This commit is contained in:
free6om 2023-12-26 14:50:24 +08:00
parent 596262d5e0
commit 4712e3a639
2 changed files with 60 additions and 0 deletions

View File

@ -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

View File

@ -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)
}