Commits

seewind  committed 21ebef7

add AccessCtrl to custom control access

  • Participants
  • Parent commits f31179c

Comments (0)

Files changed (6)

File golang/export.go

  */
 package grpc
 
+import (
+)
 
 type Exporter interface {
 	Name() string
 	hd.exports = make(map[string]Exporter)
 }
 
-func (hd *exportHandler) Register(export Exporter) (name string ) {
-	name = export.Name()
-	hd.exports[name] = export
-	return name
+func (hd *exportHandler) Register(export Exporter) {
+	hd.exports[export.Name()] = export
+	return
 }
 
 func (hd *exportHandler) UnRegister(export Exporter) {
-	name := export.Name()
-	delete(hd.exports, name)
+	delete(hd.exports, export.Name())
 }
 
 func (hd *exportHandler) GetExport(name string) (export Exporter) {

File golang/grpc.go

 	Port     int
 	sock     net.Conn
 	listener net.Listener
-	exports  map[string]interface{}
+	exports exportHandler
 }
 
 type RpcClient struct {
 	return
 }
 
-func (rpc *rpcBase) resolveAddr(addr string) bool {
-	var err error
+func (rpc *rpcBase) resolveAddr(addr string) (err error) {
 	rpc.Addr, err = resolveAddr(addr)
 	if err != nil {
-		return false
+		return err
 	}
 	rpc.SAddr = addr
-	return true
-}
-
-//close rpc
-func (rpc *rpcBase) Close() {
-	if rpc.listener != nil {
-		rpc.listener.Close()
-		rpc.listener = nil
-	}
-	if rpc.sock != nil {
-		rpc.sock.Close()
-		rpc.sock = nil
-	}
+	return err
 }
 
 //bind addr
 func (rpc *rpcBase) Bind(addr string) error {
 	var err error
 	rpc.Close()
-	ok := rpc.resolveAddr(addr)
-	if !ok {
+	err = rpc.resolveAddr(addr)
+	if err != nil {
 		return err
 	}
 	tcp_addr, ok := rpc.Addr.(*net.TCPAddr)
 }
 
 //connect to addr
-func (rpc *rpcBase) Connect(addr string) bool {
+func (rpc *rpcBase) Connect(addr string) (err error) {
 	rpc.Close()
-	ok := rpc.resolveAddr(addr)
-	if !ok {
-		return ok
+	err = rpc.resolveAddr(addr)
+	if err != nil {
+		return
 	}
-	var err error
 	tcp_addr, ok := rpc.Addr.(*net.TCPAddr)
 	if ok {
 		rpc.sock, err = net.DialTCP("tcp", nil, tcp_addr)
 		if err != nil {
-			return false
+			return
 		}
 	} else {
 		unix_addr, _ := rpc.Addr.(*net.UnixAddr)
 		rpc.sock, err = net.DialUnix("unix", nil, unix_addr)
 		if err != nil {
-			return false
+			return
 		}
 	}
-	return true
+	return
+}
+
+//close rpc
+func (rpc *rpcBase) Close() {
+	if rpc.listener != nil {
+		rpc.listener.Close()
+		rpc.listener = nil
+	}
+	if rpc.sock != nil {
+		rpc.sock.Close()
+		rpc.sock = nil
+	}
 }
 
 func (rpc *rpcBase) Start() {
 	rpc.Started = false
 }
 
+func (rpc * rpcBase) Register(export Exporter) {
+	rpc.exports.Register(export)
+}
 
 //
 //

File golang/pack.go

 }
 
 type MsgPack struct {
-	ubuf
 }
 
 func (mp *MsgPack) Pack(value interface {}) (data []byte, err error ) {
 }
 
 func (mp *MsgPack) Unpack(data []byte) (values []interface {}) {
-
+	return nil
 }
 

File grpc/__init__.py

 from .rpc_shell import *
 
 
-VERSION = (0, 3, 13)
+VERSION = (0, 3, 14)
 __version__ = VERSION
 
     def start_shell(self, console_proxy, pre_prompt):
         from rpc_shell import RpcShell
+        if self.svr.access and not self.svr.access.access_shell(self):
+            printf('[rpc]shell deny:%s', self.sock_addr)
+            return 0
+        printf('[rpc]shell start:%s', self.sock_addr)
         shell = RpcShell(self, console_proxy, pre_prompt=pre_prompt)
         shell.start()
         #shell.stop remove shell from self.shells
         return self.svc.call(0, 'execute', (func, args, kw),
                 pickle=True, **others)
 
+class AccessCtrl(object):
+    def access_sock(self, sock, addr):
+        pass
+
+    def access_shell(self, proxy):
+        pass
 
 class RpcServer(RpcBase):
     """ rpc services """
-    def __init__(self, size=None):
+    def __init__(self, size=None, access=None):
+        """ access:AccessCtrl object """
         RpcBase.__init__(self, size=size)
         self._services = {}
+        self.access = access
 
     def after_start(self):
         self._accept_task = spawn(self._accept)
             try:
                 client_socket, address = self._socket.accept()
                 def _accept_sock(client_socket, address):
+                    if self.access and \
+                            not self.access.access_sock(client_socket, address):
+                        client_socket.close()
+                        printf('[rpc]access deny:%s', address)
+                        return
+                    printf('[rpc]access:%s', address)
+
                     svc = self.init_service(client_socket)
                     if svc:
                         svc.start()

File upload.sh

File contents unchanged.