agent

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 4, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

README

NHP-Agent

架构设计

  1. Agent只与Server之间进行通信。Agent主动向Server发起短连接。不存在Agent在未建立连接时被动接收Server消息的情况。

  2. 发送消息时,向sendMsgCh发送创建好的MsgAssembler(必须指定RemoteAddr)。如果连接没有建立,agent会尝试建立并记录该连接。同时对此连接开启接收线程。MsgAssembler经过加密后会从此连接发出

  3. 接收到报文时,会将报文进行解密获取明文消息。由msghandler分别进行处理。

NHP-AGENT配置文件

etc/config.json

{
  // (mandatory) specify the private key in base64 format
  "privateKey": "+Jnee2lP6Kn47qzSaqwSmWxORsBkkCV6YHsRqXCegVo=",
  // (optional) ID of authorization service provider this agent belongs to
  "aspId": "abc_group",
  // (mandatory) an user object is necessary to carry out knock requests
  "user": {
    "userId": "zengl",
    "devId": "0123456789abcdef",
    "orgId": "abc.com.cn"
  },
  // preset resources to begin knock after start
  "knockRess": [
    {
      "aspId": "abc_group",
      "resId": "app_resource_group_001",
      "serverKey": "WqJxe+Z4+wLen3VRgZx6YnbjvJFmptz99zkONCt/7gc="
    }
  ],
  // list of preset allowed server peers to send knock request. It has the same effect as AddServer()
  "servers": [
    {
      // (optional) the server's hostname. Its resolved address overrides the "Ip" field
      "host": "",
      // IP address of the server peer
      "ip": "192.168.80.35",
      // listening port for the server peer
      "port": 62206,
      // type: NHP-Server
      "type": 2,
      // public key in base64 format
      "pubKeyBase64": "WqJxe+Z4+wLen3VRgZx6YnbjvJFmptz99zkONCt/7gc=",
      /// expire time for the public key (seconds from epoch)
      "expireTime": 1716345064
    }
  ]
}

Documentation

Index

Constants

View Source
const (
	MaxConcurrentConnection      = 256
	DefaultConnectionTimeoutMs   = 30 * 1000 // 30 seconds to delete idle connection
	PacketQueueSizePerConnection = 8         // nhp agent does not need large transactions
)

Variables

View Source
var (
	ExeDirPath string
)

Functions

This section is empty.

Types

type Config

type Config struct {
	LogLevel         int
	PrivateKeyBase64 string
	KnockUser        `mapstructure:",squash"`
}

type KnockResource

type KnockResource struct {
	AuthServiceId string `json:"aspId"`
	ResourceId    string `json:"resId"`
	ServerAddr    string `json:"serverAddr"`
}

func (*KnockResource) Id

func (res *KnockResource) Id() string

type KnockTarget

type KnockTarget struct {
	sync.Mutex
	KnockResource
	ServerPeer           *core.UdpPeer
	LastKnockSuccessTime time.Time
}

func (*KnockTarget) Server

func (kt *KnockTarget) Server() *core.UdpPeer

func (*KnockTarget) SetResource

func (kt *KnockTarget) SetResource(res *KnockResource)

func (*KnockTarget) SetServer

func (kt *KnockTarget) SetServer(peer *core.UdpPeer)

type KnockUser

type KnockUser struct {
	UserId         string
	OrganizationId string
	UserData       map[string]any
}

type Peers

type Peers struct {
	Servers []*core.UdpPeer
}

type Resources

type Resources struct {
	Resources []*KnockResource
}

type UdpAgent

type UdpAgent struct {
	// contains filtered or unexported fields
}

func (*UdpAgent) AddResource

func (a *UdpAgent) AddResource(res *KnockResource) error

func (*UdpAgent) AddServer

func (a *UdpAgent) AddServer(server *core.UdpPeer)

func (*UdpAgent) ExitKnockRequest

func (a *UdpAgent) ExitKnockRequest(res *KnockTarget) (ackMsg *common.ServerKnockAckMsg, err error)

func (*UdpAgent) FindServerPeerFromResource

func (a *UdpAgent) FindServerPeerFromResource(res *KnockResource) *core.UdpPeer

func (*UdpAgent) HandleCookieMessage

func (a *UdpAgent) HandleCookieMessage(ppd *core.PacketParserData) bool

func (*UdpAgent) IsRunning

func (a *UdpAgent) IsRunning() bool

func (*UdpAgent) Knock

func (a *UdpAgent) Knock(res *KnockTarget) (ackMsg *common.ServerKnockAckMsg, err error)

func (*UdpAgent) ListResource

func (a *UdpAgent) ListResource(target *KnockTarget) (lrtMsg *common.ServerListResultMsg, err error)

func (*UdpAgent) RegisterPublicKey

func (a *UdpAgent) RegisterPublicKey(otp string, target *KnockTarget) (rakMsg *common.ServerRegisterAckMsg, err error)

func (*UdpAgent) RemoveResource

func (a *UdpAgent) RemoveResource(aspId string, resId string)

func (*UdpAgent) RemoveServer

func (a *UdpAgent) RemoveServer(serverKey string)

func (*UdpAgent) RequestOtp

func (a *UdpAgent) RequestOtp(target *KnockTarget) error

func (*UdpAgent) ResolvePeer

func (a *UdpAgent) ResolvePeer(peer *core.UdpPeer) (*core.UdpPeer, net.Addr)

if the server uses hostname as destination, find the correct peer with the actual IP address

func (*UdpAgent) SendPacket

func (a *UdpAgent) SendPacket(pkt *core.Packet, conn *UdpConn) (n int, err error)

func (*UdpAgent) SetCheckResults

func (a *UdpAgent) SetCheckResults(results map[string]any)

func (*UdpAgent) SetDeviceId

func (a *UdpAgent) SetDeviceId(devId string)

func (*UdpAgent) SetKnockUser

func (a *UdpAgent) SetKnockUser(usrId string, orgId string, userData map[string]any)

func (*UdpAgent) Start

func (a *UdpAgent) Start(dirPath string, logLevel int) (err error)

dirPath: the path of app or shared library entry point logLevel: 0: silent, 1: error, 2: info, 3: debug, 4: verbose

func (*UdpAgent) StartKnockLoop

func (a *UdpAgent) StartKnockLoop() int

func (*UdpAgent) Stop

func (a *UdpAgent) Stop()

export Stop

func (*UdpAgent) StopConfigWatch

func (a *UdpAgent) StopConfigWatch()

func (*UdpAgent) StopKnockLoop

func (a *UdpAgent) StopKnockLoop()

type UdpConn

type UdpConn struct {
	ConnData *core.ConnectionData
	// contains filtered or unexported fields
}

func (*UdpConn) Close

func (c *UdpConn) Close()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL