mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-12 20:03:38 +00:00
32 lines
528 B
Go
32 lines
528 B
Go
package option
|
|
|
|
import (
|
|
"net/netip"
|
|
|
|
"github.com/goccy/go-json"
|
|
)
|
|
|
|
type ListenAddress netip.Addr
|
|
|
|
func (a ListenAddress) MarshalJSON() ([]byte, error) {
|
|
addr := netip.Addr(a)
|
|
if !addr.IsValid() {
|
|
return json.Marshal("")
|
|
}
|
|
return json.Marshal(addr.String())
|
|
}
|
|
|
|
func (a *ListenAddress) UnmarshalJSON(bytes []byte) error {
|
|
var value string
|
|
err := json.Unmarshal(bytes, &value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
addr, err := netip.ParseAddr(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*a = ListenAddress(addr)
|
|
return nil
|
|
}
|