| 1 | package exitpolicy |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/netip" |
| 6 | "regexp" |
| 7 | "strings" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | |
| 13 | type Policy struct { |
| 14 | Mode string `json:"mode"` |
| 15 | Scope string `json:"scope"` |
| 16 | IP string `json:"ip,omitempty"` |
| 17 | CountryCode string `json:"country_code,omitempty"` |
| 18 | Region string `json:"region,omitempty"` |
| 19 | City string `json:"city,omitempty"` |
| 20 | } |
| 21 | |
| 22 | type Location struct { |
| 23 | IP, CountryCode, Region, City string |
| 24 | Country, RegionCode, Timezone string |
| 25 | CheckedAt time.Time |
| 26 | } |
| 27 | |
| 28 | type State string |
| 29 | |
| 30 | const ( |
| 31 | Passed State = "passed" |
| 32 | Failed State = "failed" |
| 33 | Unknown State = "unknown" |
| 34 | ) |
| 35 | |
| 36 | func (p Policy) Normalized() Policy { |
| 37 | if p.Mode == "" { |
| 38 | p.Mode = "dynamic" |
| 39 | } |
| 40 | if p.Scope == "" { |
| 41 | p.Scope = "country" |
| 42 | } |
| 43 | p.IP = strings.TrimSpace(p.IP) |
| 44 | p.CountryCode = strings.ToUpper(strings.TrimSpace(p.CountryCode)) |
| 45 | p.Region = strings.Join(strings.Fields(p.Region), " ") |
| 46 | p.City = strings.Join(strings.Fields(p.City), " ") |
| 47 | return p |
| 48 | } |
| 49 | |
| 50 | func (p Policy) Validate() error { |
| 51 | p = p.Normalized() |
| 52 | if p.Mode != "static" && p.Mode != "dynamic" { |
| 53 | return errors.New("请选择静态或动态 IP") |
| 54 | } |
| 55 | if p.Scope != "any" && p.Scope != "country" && p.Scope != "region" && p.Scope != "city" { |
| 56 | return errors.New("请选择动态 IP 的允许范围") |
| 57 | } |
| 58 | if p.IP != "" { |
| 59 | ip, err := netip.ParseAddr(p.IP) |
| 60 | if err != nil || !ip.Is4() || !ip.IsGlobalUnicast() || ip.IsPrivate() { |
| 61 | return errors.New("固定出口应填写公网 IPv4 地址") |
| 62 | } |
| 63 | } |
| 64 | if p.CountryCode != "" && !regexp.MustCompile(`^[A-Z]{2}$`).MatchString(p.CountryCode) { |
| 65 | return errors.New("国家或地区代码应为两个字母,例如 US") |
| 66 | } |
| 67 | if len(p.Region) > 120 || len(p.City) > 120 { |
| 68 | return errors.New("地区名称过长") |
| 69 | } |
| 70 | return nil |
| 71 | } |
| 72 | |
| 73 | func (p Policy) NeedsBinding() bool { |
| 74 | p = p.Normalized() |
| 75 | if p.Mode == "static" { |
| 76 | return p.IP == "" |
| 77 | } |
| 78 | return p.Scope != "any" && (p.CountryCode == "" || (p.Scope == "region" || p.Scope == "city") && p.Region == "" || p.Scope == "city" && p.City == "") |
| 79 | } |
| 80 | |
| 81 | |
| 82 | |
| 83 | func (p Policy) BindMissing(location Location) Policy { |
| 84 | p = p.Normalized() |
| 85 | if state, _ := CheckExitCountry(location.IP, &location); state != Passed { |
| 86 | return p |
| 87 | } |
| 88 | if p.Mode == "static" && p.IP == "" { |
| 89 | p.IP = location.IP |
| 90 | } |
| 91 | if p.Mode == "dynamic" && p.Scope != "any" { |
| 92 | if p.CountryCode == "" { |
| 93 | p.CountryCode = location.CountryCode |
| 94 | } |
| 95 | if (p.Scope == "region" || p.Scope == "city") && strings.EqualFold(p.CountryCode, location.CountryCode) { |
| 96 | if p.Region == "" { |
| 97 | p.Region = location.Region |
| 98 | } |
| 99 | if p.Scope == "city" && p.City == "" { |
| 100 | p.City = location.City |
| 101 | } |
| 102 | } |
| 103 | } |
| 104 | return p.Normalized() |
| 105 | } |
| 106 | |
| 107 | func (p Policy) Evaluate(ip string, location *Location) (State, string) { |
| 108 | p = p.Normalized() |
| 109 | if err := p.Validate(); err != nil { |
| 110 | return Unknown, err.Error() |
| 111 | } |
| 112 | if state, message := CheckExitCountry(ip, location); state != Passed { |
| 113 | return state, message |
| 114 | } |
| 115 | if p.NeedsBinding() { |
| 116 | if p.Mode == "dynamic" && p.CountryCode == "" { |
| 117 | return Unknown, "动态 IP 的允许国家或地区尚未绑定,外网保持关闭。请在网络与代理设置中确认出口地区" |
| 118 | } |
| 119 | return Unknown, "尚未绑定出口规则,请在设置中确认检测到的 IP 或地区" |
| 120 | } |
| 121 | if p.Mode == "static" { |
| 122 | if p.IP != ip { |
| 123 | return Failed, "检测出口已偏离绑定的静态 IP" |
| 124 | } |
| 125 | return Passed, "检测出口与绑定的静态 IP 一致" |
| 126 | } |
| 127 | if p.Scope == "any" { |
| 128 | return Passed, "动态 IP 可在非中国大陆地区变化,流量仍须经指定代理" |
| 129 | } |
| 130 | if !strings.EqualFold(p.CountryCode, location.CountryCode) { |
| 131 | return Failed, "检测出口超出允许的国家或地区" |
| 132 | } |
| 133 | if p.Scope == "country" { |
| 134 | return Passed, "检测出口位于允许的国家或地区" |
| 135 | } |
| 136 | if location.Region == "" { |
| 137 | return Unknown, "当前位置缺少州或省,不能确认允许范围" |
| 138 | } |
| 139 | if !strings.EqualFold(p.Region, strings.Join(strings.Fields(location.Region), " ")) { |
| 140 | return Failed, "检测出口超出允许的州或省范围" |
| 141 | } |
| 142 | if p.Scope == "region" { |
| 143 | return Passed, "检测出口位于允许的州或省" |
| 144 | } |
| 145 | if location.City == "" { |
| 146 | return Unknown, "当前位置缺少州、省或城市,不能确认城市范围" |
| 147 | } |
| 148 | if !strings.EqualFold(p.City, strings.Join(strings.Fields(location.City), " ")) { |
| 149 | return Failed, "检测出口超出允许的城市范围" |
| 150 | } |
| 151 | return Passed, "检测出口位于允许的城市及其所属州、省" |
| 152 | } |