1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-17 04:55:23 +00:00

remove unnecessary code

This commit is contained in:
Darien Raymond 2017-01-27 22:49:54 +01:00
parent de77b89bb6
commit 09e5ae7a4b
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
2 changed files with 5 additions and 90 deletions

View File

@ -13,15 +13,13 @@ import (
)
var (
ErrInvalidRule = errors.New("Invalid Rule")
ErrNoRuleApplicable = errors.New("No rule applicable")
)
type Router struct {
domainStrategy Config_DomainStrategy
rules []Rule
// cache *RoutingTable
dnsServer dns.Server
dnsServer dns.Server
}
func NewRouter(ctx context.Context, config *Config) (*Router, error) {
@ -31,8 +29,7 @@ func NewRouter(ctx context.Context, config *Config) (*Router, error) {
}
r := &Router{
domainStrategy: config.DomainStrategy,
//cache: NewRoutingTable(),
rules: make([]Rule, len(config.Rule)),
rules: make([]Rule, len(config.Rule)),
}
space.OnInitialize(func() error {
@ -54,8 +51,7 @@ func NewRouter(ctx context.Context, config *Config) (*Router, error) {
return r, nil
}
// Private: Visible for testing.
func (v *Router) ResolveIP(dest net.Destination) []net.Address {
func (v *Router) resolveIP(dest net.Destination) []net.Address {
ips := v.dnsServer.Get(dest.Address.Domain())
if len(ips) == 0 {
return nil
@ -67,7 +63,7 @@ func (v *Router) ResolveIP(dest net.Destination) []net.Address {
return dests
}
func (v *Router) takeDetourWithoutCache(ctx context.Context) (string, error) {
func (v *Router) TakeDetour(ctx context.Context) (string, error) {
for _, rule := range v.rules {
if rule.Apply(ctx) {
return rule.Tag, nil
@ -77,7 +73,7 @@ func (v *Router) takeDetourWithoutCache(ctx context.Context) (string, error) {
dest := proxy.DestinationFromContext(ctx)
if v.domainStrategy == Config_IpIfNonMatch && dest.Address.Family().IsDomain() {
log.Info("Router: Looking up IP for ", dest)
ipDests := v.ResolveIP(dest)
ipDests := v.resolveIP(dest)
if ipDests != nil {
ctx = proxy.ContextWithResolveIPs(ctx, ipDests)
for _, rule := range v.rules {
@ -91,17 +87,6 @@ func (v *Router) takeDetourWithoutCache(ctx context.Context) (string, error) {
return "", ErrNoRuleApplicable
}
func (v *Router) TakeDetour(ctx context.Context) (string, error) {
//destStr := dest.String()
//found, tag, err := v.cache.Get(destStr)
//if !found {
tag, err := v.takeDetourWithoutCache(ctx)
//v.cache.Set(destStr, tag, err)
return tag, err
//}
//return tag, err
}
func (Router) Interface() interface{} {
return (*Router)(nil)
}

View File

@ -1,70 +0,0 @@
package router
import (
"sync"
"time"
)
type RoutingEntry struct {
tag string
err error
expire time.Time
}
func (v *RoutingEntry) Extend() {
v.expire = time.Now().Add(time.Hour)
}
func (v *RoutingEntry) Expired() bool {
return v.expire.Before(time.Now())
}
type RoutingTable struct {
sync.RWMutex
table map[string]*RoutingEntry
}
func NewRoutingTable() *RoutingTable {
return &RoutingTable{
table: make(map[string]*RoutingEntry),
}
}
func (v *RoutingTable) Cleanup() {
v.Lock()
defer v.Unlock()
for key, value := range v.table {
if value.Expired() {
delete(v.table, key)
}
}
}
func (v *RoutingTable) Set(destination string, tag string, err error) {
v.Lock()
defer v.Unlock()
entry := &RoutingEntry{
tag: tag,
err: err,
}
entry.Extend()
v.table[destination] = entry
if len(v.table) > 1000 {
go v.Cleanup()
}
}
func (v *RoutingTable) Get(destination string) (bool, string, error) {
v.RLock()
defer v.RUnlock()
entry, found := v.table[destination]
if !found {
return false, "", nil
}
entry.Extend()
return true, entry.tag, entry.err
}