Skip to content

Commit b6de191

Browse files
author
seanchann
committed
1.fix iptables bug. 2 import k8s package
1 parent 1cdb726 commit b6de191

File tree

21 files changed

+2114
-16
lines changed

21 files changed

+2114
-16
lines changed

pkg/access/firewall.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import (
77

88
"github.com/golang/glog"
99

10-
utildbus "k8s.io/kubernetes/pkg/util/dbus"
11-
utilexec "k8s.io/kubernetes/pkg/util/exec"
12-
utiliptables "k8s.io/kubernetes/pkg/util/iptables"
10+
utildbus "shadowss/pkg/util/dbus"
11+
utilexec "shadowss/pkg/util/exec"
12+
utiliptables "shadowss/pkg/util/iptables"
1313
)
1414

1515
var IptablesHandler utiliptables.Interface
1616

1717
const (
18-
jumpAccept = "ACCEPT"
19-
jumpDrop = "DROP"
18+
jumpAccept = "ACCEPT"
19+
jumpDrop = "DROP"
20+
publicAllowChain = "IN_public_allow"
2021
)
2122

2223
func init() {
@@ -36,13 +37,15 @@ func OpenLocalPort(port int, protocol string) error {
3637
"-m", "comment", "--comment", fmt.Sprintf(`"%s hostport %d"`, "shadowss", port),
3738
"-m", protocol, "-p", protocol,
3839
"--dport", fmt.Sprintf("%d", port),
40+
"-m", "conntrack", "--ctstate", "NEW",
3941
"-j", string(jumpAccept),
4042
}
4143
writeLine(portRules, args...)
4244

4345
natLines := portRules.Bytes()
44-
glog.V(3).Infof("Restoring iptables rules: %s", natLines)
45-
ok, err := IptablesHandler.EnsureRule(utiliptables.Append, utiliptables.TableFilter, utiliptables.ChainInput, args...)
46+
glog.V(3).Infof("ensure iptables rules: %s", natLines)
47+
48+
ok, err := IptablesHandler.EnsureRule(utiliptables.Append, utiliptables.TableFilter, utiliptables.Chain(publicAllowChain), args...)
4649
if err != nil {
4750
return fmt.Errorf("Failed to execute iptables: %v", err)
4851
}
@@ -58,13 +61,14 @@ func TurnoffLocalPort(port int, protocol string) error {
5861
"-m", "comment", "--comment", fmt.Sprintf(`"%s hostport %d"`, "shadowss", port),
5962
"-m", protocol, "-p", protocol,
6063
"--dport", fmt.Sprintf("%d", port),
64+
"-m", "conntrack", "--ctstate", "NEW",
6165
"-j", string(jumpAccept)}
6266

6367
writeLine(portRules, args...)
6468

6569
natLines := portRules.Bytes()
6670
glog.V(3).Infof("Ensure iptables rules: %s", natLines)
67-
err := IptablesHandler.DeleteRule(utiliptables.TableFilter, utiliptables.ChainInput, args...)
71+
err := IptablesHandler.DeleteRule(utiliptables.TableFilter, utiliptables.Chain(publicAllowChain), args...)
6872
if err != nil {
6973
return fmt.Errorf("Failed to execute iptables : %v", err)
7074
}

pkg/connection/tcp/tcp_server.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ func (tcpSrv *TCPServer) handleRequest(ctx context.Context, acceptConn net.Conn)
142142

143143
//Run start a tcp listen for user
144144
func (tcpSrv *TCPServer) Run() {
145-
port := tcpSrv.Config.Port
145+
//allways realloc port for user
146+
port := 0
146147

147148
portStr := strconv.Itoa(port)
148149
ln, err := net.Listen("tcp", ":"+portStr)

pkg/util/dbus/dbus.go

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Copyright 2015 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package dbus
18+
19+
import (
20+
godbus "github.com/godbus/dbus"
21+
)
22+
23+
// Interface is an interface that presents a subset of the godbus/dbus API. Use this
24+
// when you want to inject fakeable/mockable D-Bus behavior.
25+
type Interface interface {
26+
// SystemBus returns a connection to the system bus, connecting to it
27+
// first if necessary
28+
SystemBus() (Connection, error)
29+
// SessionBus returns a connection to the session bus, connecting to it
30+
// first if necessary
31+
SessionBus() (Connection, error)
32+
}
33+
34+
// Connection represents a D-Bus connection
35+
type Connection interface {
36+
// Returns an Object representing the bus itself
37+
BusObject() Object
38+
39+
// Object creates a representation of a remote D-Bus object
40+
Object(name, path string) Object
41+
42+
// Signal registers or unregisters a channel to receive D-Bus signals
43+
Signal(ch chan<- *godbus.Signal)
44+
}
45+
46+
// Object represents a remote D-Bus object
47+
type Object interface {
48+
// Call synchronously calls a D-Bus method
49+
Call(method string, flags godbus.Flags, args ...interface{}) Call
50+
}
51+
52+
// Call represents a pending or completed D-Bus method call
53+
type Call interface {
54+
// Store returns a completed call's return values, or an error
55+
Store(retvalues ...interface{}) error
56+
}
57+
58+
// Implements Interface in terms of actually talking to D-Bus
59+
type dbusImpl struct {
60+
systemBus *connImpl
61+
sessionBus *connImpl
62+
}
63+
64+
// Implements Connection as a godbus.Conn
65+
type connImpl struct {
66+
conn *godbus.Conn
67+
}
68+
69+
// Implements Object as a godbus.Object
70+
type objectImpl struct {
71+
object godbus.BusObject
72+
}
73+
74+
// Implements Call as a godbus.Call
75+
type callImpl struct {
76+
call *godbus.Call
77+
}
78+
79+
// New returns a new Interface which will use godbus to talk to D-Bus
80+
func New() Interface {
81+
return &dbusImpl{}
82+
}
83+
84+
// SystemBus is part of Interface
85+
func (db *dbusImpl) SystemBus() (Connection, error) {
86+
if db.systemBus == nil {
87+
bus, err := godbus.SystemBus()
88+
if err != nil {
89+
return nil, err
90+
}
91+
db.systemBus = &connImpl{bus}
92+
}
93+
94+
return db.systemBus, nil
95+
}
96+
97+
// SessionBus is part of Interface
98+
func (db *dbusImpl) SessionBus() (Connection, error) {
99+
if db.sessionBus == nil {
100+
bus, err := godbus.SessionBus()
101+
if err != nil {
102+
return nil, err
103+
}
104+
db.sessionBus = &connImpl{bus}
105+
}
106+
107+
return db.sessionBus, nil
108+
}
109+
110+
// BusObject is part of the Connection interface
111+
func (conn *connImpl) BusObject() Object {
112+
return &objectImpl{conn.conn.BusObject()}
113+
}
114+
115+
// Object is part of the Connection interface
116+
func (conn *connImpl) Object(name, path string) Object {
117+
return &objectImpl{conn.conn.Object(name, godbus.ObjectPath(path))}
118+
}
119+
120+
// Signal is part of the Connection interface
121+
func (conn *connImpl) Signal(ch chan<- *godbus.Signal) {
122+
conn.conn.Signal(ch)
123+
}
124+
125+
// Call is part of the Object interface
126+
func (obj *objectImpl) Call(method string, flags godbus.Flags, args ...interface{}) Call {
127+
return &callImpl{obj.object.Call(method, flags, args...)}
128+
}
129+
130+
// Store is part of the Call interface
131+
func (call *callImpl) Store(retvalues ...interface{}) error {
132+
return call.call.Store(retvalues...)
133+
}

0 commit comments

Comments
 (0)