Darien Raymond пре 7 година
родитељ
комит
39cfc982b5
4 измењених фајлова са 43 додато и 28 уклоњено
  1. 0 18
      app/policy/config.go
  2. 5 9
      app/policy/manager.go
  3. 37 0
      app/policy/manager_test.go
  4. 1 1
      policy.go

+ 0 - 18
app/policy/config.go

@@ -14,24 +14,6 @@ func (s *Second) Duration() time.Duration {
 	return time.Second * time.Duration(s.Value)
 }
 
-// OverrideWith overrides current Policy with another one.
-func (p *Policy) OverrideWith(another *Policy) {
-	if another.Timeout != nil {
-		if another.Timeout.Handshake != nil {
-			p.Timeout.Handshake = another.Timeout.Handshake
-		}
-		if another.Timeout.ConnectionIdle != nil {
-			p.Timeout.ConnectionIdle = another.Timeout.ConnectionIdle
-		}
-		if another.Timeout.UplinkOnly != nil {
-			p.Timeout.UplinkOnly = another.Timeout.UplinkOnly
-		}
-		if another.Timeout.DownlinkOnly != nil {
-			p.Timeout.DownlinkOnly = another.Timeout.DownlinkOnly
-		}
-	}
-}
-
 func (p *Policy) ToCorePolicy() core.Policy {
 	var cp core.Policy
 	if p.Timeout != nil {

+ 5 - 9
app/policy/manager.go

@@ -19,19 +19,15 @@ func New(ctx context.Context, config *Config) (*Instance, error) {
 	}
 	if len(config.Level) > 0 {
 		for lv, p := range config.Level {
-			dp := core.DefaultPolicy()
-			dp.OverrideWith(p.ToCorePolicy())
-			m.levels[lv] = dp
+			m.levels[lv] = p.ToCorePolicy().OverrideWith(core.DefaultPolicy())
 		}
 	}
 
 	v := core.FromContext(ctx)
-	if v == nil {
-		return nil, newError("V is not in context.")
-	}
-
-	if err := v.RegisterFeature((*core.PolicyManager)(nil), m); err != nil {
-		return nil, newError("unable to register PolicyManager in core").Base(err).AtError()
+	if v != nil {
+		if err := v.RegisterFeature((*core.PolicyManager)(nil), m); err != nil {
+			return nil, newError("unable to register PolicyManager in core").Base(err).AtError()
+		}
 	}
 
 	return m, nil

+ 37 - 0
app/policy/manager_test.go

@@ -0,0 +1,37 @@
+package policy_test
+
+import (
+	"context"
+	"testing"
+	"time"
+
+	"v2ray.com/core"
+	. "v2ray.com/core/app/policy"
+	. "v2ray.com/ext/assert"
+)
+
+func TestPolicy(t *testing.T) {
+	assert := With(t)
+
+	manager, err := New(context.Background(), &Config{
+		Level: map[uint32]*Policy{
+			0: &Policy{
+				Timeout: &Policy_Timeout{
+					Handshake: &Second{
+						Value: 2,
+					},
+				},
+			},
+		},
+	})
+	assert(err, IsNil)
+
+	pDefault := core.DefaultPolicy()
+
+	p0 := manager.ForLevel(0)
+	assert(p0.Timeouts.Handshake, Equals, 2*time.Second)
+	assert(p0.Timeouts.ConnectionIdle, Equals, pDefault.Timeouts.ConnectionIdle)
+
+	p1 := manager.ForLevel(1)
+	assert(p1.Timeouts.Handshake, Equals, pDefault.Timeouts.Handshake)
+}

+ 1 - 1
policy.go

@@ -43,7 +43,7 @@ type Policy struct {
 
 // OverrideWith overrides the current Policy with another one. All values with default value will be overridden.
 func (p Policy) OverrideWith(another Policy) Policy {
-	p.Timeouts.OverrideWith(another.Timeouts)
+	p.Timeouts = p.Timeouts.OverrideWith(another.Timeouts)
 	return p
 }