2022-07-02 06:07:50 +00:00
|
|
|
package option
|
2022-06-30 13:27:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
|
|
|
C "github.com/sagernet/sing-box/constant"
|
|
|
|
E "github.com/sagernet/sing/common/exceptions"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NetworkList []string
|
|
|
|
|
|
|
|
func (v *NetworkList) UnmarshalJSON(data []byte) error {
|
2022-07-02 14:55:10 +00:00
|
|
|
var networkList []string
|
2022-06-30 13:27:56 +00:00
|
|
|
err := json.Unmarshal(data, &networkList)
|
|
|
|
if err != nil {
|
2022-07-02 14:55:10 +00:00
|
|
|
var networkItem string
|
|
|
|
err = json.Unmarshal(data, &networkItem)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
networkList = []string{networkItem}
|
2022-06-30 13:27:56 +00:00
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
for _, networkName := range networkList {
|
2022-06-30 13:27:56 +00:00
|
|
|
switch networkName {
|
|
|
|
case "tcp", "udp":
|
|
|
|
default:
|
|
|
|
return E.New("unknown network: " + networkName)
|
|
|
|
}
|
|
|
|
}
|
2022-07-02 14:55:10 +00:00
|
|
|
*v = networkList
|
2022-06-30 13:27:56 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *NetworkList) Build() []string {
|
|
|
|
if len(*v) == 0 {
|
|
|
|
return []string{C.NetworkTCP, C.NetworkUDP}
|
|
|
|
}
|
|
|
|
return *v
|
|
|
|
}
|