1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-08 11:17:44 -05:00
v2fly/common/net/json/network.go

47 lines
943 B
Go
Raw Normal View History

2015-10-30 10:56:46 -04:00
package json
import (
"encoding/json"
2015-11-01 17:01:15 -05:00
"errors"
2015-10-30 10:56:46 -04:00
"strings"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type NetworkList []string
2015-11-01 17:01:15 -05:00
func NewNetworkList(networks []string) NetworkList {
list := NetworkList(make([]string, len(networks)))
for idx, network := range networks {
list[idx] = strings.ToLower(strings.TrimSpace(network))
}
return list
}
2015-10-30 10:56:46 -04:00
func (this *NetworkList) UnmarshalJSON(data []byte) error {
var strList []string
err := json.Unmarshal(data, &strList)
2015-11-01 17:01:15 -05:00
if err == nil {
*this = NewNetworkList(strList)
return nil
2015-10-30 10:56:46 -04:00
}
2015-11-01 17:01:15 -05:00
var str string
err = json.Unmarshal(data, &str)
if err == nil {
strList := strings.Split(str, ",")
*this = NewNetworkList(strList)
return nil
2015-10-30 10:56:46 -04:00
}
2015-11-01 17:01:15 -05:00
return errors.New("Unknown format of network list: " + string(data))
2015-10-30 10:56:46 -04:00
}
func (this *NetworkList) HasNetwork(network v2net.Network) bool {
for _, value := range *this {
if value == string(network) {
return true
}
}
return false
}