pull/314/head v2.3.2
Darien Raymond 8 years ago
parent 8b936bc816
commit 4a67587873
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169

@ -10,6 +10,8 @@ import (
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/tools/geoip"
"github.com/golang/protobuf/proto"
)
type RouterRulesConfig struct {
@ -173,9 +175,13 @@ func parseChinaIPRule(data []byte) (*router.RoutingRule, error) {
log.Error("Router: Invalid router rule: ", err)
return nil, err
}
var chinaIPs geoip.CountryIPRange
if err := proto.Unmarshal(geoip.ChinaIPs, &chinaIPs); err != nil {
return nil, err
}
return &router.RoutingRule{
Tag: rawRule.OutboundTag,
Ip: geoip.ChinaIPs,
Ip: chinaIPs.Ips,
}, nil
}

@ -0,0 +1,66 @@
// Code generated by protoc-gen-go.
// source: v2ray.com/core/tools/geoip/geoip.proto
// DO NOT EDIT!
/*
Package geoip is a generated protocol buffer package.
It is generated from these files:
v2ray.com/core/tools/geoip/geoip.proto
It has these top-level messages:
CountryIPRange
*/
package geoip
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import v2ray_core_app_router "v2ray.com/core/app/router"
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
// A compilation error at this line likely means your copy of the
// proto package needs to be updated.
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
type CountryIPRange struct {
Ips []*v2ray_core_app_router.IP `protobuf:"bytes,1,rep,name=ips" json:"ips,omitempty"`
}
func (m *CountryIPRange) Reset() { *m = CountryIPRange{} }
func (m *CountryIPRange) String() string { return proto.CompactTextString(m) }
func (*CountryIPRange) ProtoMessage() {}
func (*CountryIPRange) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
func (m *CountryIPRange) GetIps() []*v2ray_core_app_router.IP {
if m != nil {
return m.Ips
}
return nil
}
func init() {
proto.RegisterType((*CountryIPRange)(nil), "v2ray.core.tools.geoip.CountryIPRange")
}
func init() { proto.RegisterFile("v2ray.com/core/tools/geoip/geoip.proto", fileDescriptor0) }
var fileDescriptor0 = []byte{
// 153 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x52, 0x2b, 0x33, 0x2a, 0x4a,
0xac, 0xd4, 0x4b, 0xce, 0xcf, 0xd5, 0x4f, 0xce, 0x2f, 0x4a, 0xd5, 0x2f, 0xc9, 0xcf, 0xcf, 0x29,
0xd6, 0x4f, 0x4f, 0xcd, 0xcf, 0x2c, 0x80, 0x90, 0x7a, 0x05, 0x45, 0xf9, 0x25, 0xf9, 0x42, 0x62,
0x30, 0x75, 0x45, 0xa9, 0x7a, 0x60, 0x35, 0x7a, 0x60, 0x59, 0x29, 0x74, 0xfd, 0x89, 0x05, 0x05,
0xfa, 0x45, 0xf9, 0xa5, 0x25, 0xa9, 0x45, 0xfa, 0xc9, 0xf9, 0x79, 0x69, 0x99, 0xe9, 0x10, 0xfd,
0x4a, 0xb6, 0x5c, 0x7c, 0xce, 0xf9, 0xa5, 0x79, 0x25, 0x45, 0x95, 0x9e, 0x01, 0x41, 0x89, 0x79,
0xe9, 0xa9, 0x42, 0xda, 0x5c, 0xcc, 0x99, 0x05, 0xc5, 0x12, 0x8c, 0x0a, 0xcc, 0x1a, 0xdc, 0x46,
0x92, 0x7a, 0x48, 0xe6, 0x27, 0x16, 0x14, 0xe8, 0x41, 0xcc, 0xd0, 0xf3, 0x0c, 0x08, 0x02, 0xa9,
0x72, 0x62, 0x8f, 0x62, 0x05, 0xdb, 0x97, 0xc4, 0x06, 0x36, 0xce, 0x18, 0x10, 0x00, 0x00, 0xff,
0xff, 0x60, 0x87, 0xf6, 0x5e, 0xb8, 0x00, 0x00, 0x00,
}

@ -0,0 +1,10 @@
syntax = "proto3";
package v2ray.core.tools.geoip;
option go_package = "geoip";
import "v2ray.com/core/app/router/config.proto";
message CountryIPRange {
repeated v2ray.core.app.router.IP ips = 1;
}

File diff suppressed because one or more lines are too long

@ -12,17 +12,17 @@ import (
"os"
"strconv"
"strings"
"v2ray.com/core/app/router"
"v2ray.com/core/tools/geoip"
"github.com/golang/protobuf/proto"
)
const (
apnicFile = "http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest"
)
type IPEntry struct {
IP []byte
Bits uint32
}
func main() {
resp, err := http.Get(apnicFile)
if err != nil {
@ -34,7 +34,9 @@ func main() {
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
ips := make([]IPEntry, 0, 8192)
ips := &geoip.CountryIPRange{
Ips: make([]*router.IP, 0, 8192),
}
for scanner.Scan() {
line := scanner.Text()
line = strings.TrimSpace(line)
@ -55,12 +57,17 @@ func main() {
if len(ipBytes) == 0 {
panic("Invalid IP " + ip)
}
ips = append(ips, IPEntry{
IP: []byte(ipBytes),
Bits: mask,
ips.Ips = append(ips.Ips, &router.IP{
Ip: []byte(ipBytes)[12:16],
UnmatchingBits: mask,
})
}
ipbytes, err := proto.Marshal(ips)
if err != nil {
log.Fatalf("Failed to marshal country IPs: %v", err)
}
file, err := os.OpenFile("geoip_data.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to generate geoip_data.go: %v", err)
@ -68,18 +75,8 @@ func main() {
defer file.Close()
fmt.Fprintln(file, "package geoip")
fmt.Fprintln(file, "import \"v2ray.com/core/app/router\"")
fmt.Fprintln(file, "var ChinaIPs []*router.IP")
fmt.Fprintln(file, "func init() {")
fmt.Fprintln(file, "ChinaIPs = []*router.IP {")
for _, ip := range ips {
fmt.Fprintln(file, "&router.IP{", formatArray(ip.IP[12:16]), ",", ip.Bits, "},")
}
fmt.Fprintln(file, "}")
fmt.Fprintln(file, "}")
fmt.Fprintln(file, "var ChinaIPs = "+formatArray(ipbytes))
}
func formatArray(a []byte) string {

Loading…
Cancel
Save