Skip to content

Commit

Permalink
update code structure and symbols to be more self-explanatory.
Browse files Browse the repository at this point in the history
# Conflicts:
#	ac/config.go
#	ac/main/main.go
#	ac/msghandler.go
#	ac/udpac.go
#	agent/config.go
#	agent/knock.go
#	agent/main/export.go
#	agent/main/main.go
#	agent/msghandler.go
#	agent/request.go
#	agent/udpagent.go
#	core/benchmark/ecc_rsa_test.go
#	core/initiator.go
#	core/responder.go
#	nhp/packet.go
#	server/config.go
#	server/main/main.go
#	server/msghandler.go
#	server/nhpauth.go
#	server/udpserver.go
#	test/ecdh_test.go
#	test/ecdsa_test.go
#	test/packet_test.go
#	test/toml_test.go
  • Loading branch information
craftleon committed Sep 4, 2024
1 parent d676158 commit 4925f96
Show file tree
Hide file tree
Showing 55 changed files with 2,485 additions and 1,388 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ generate-version-and-build:
@$(MAKE) acd
@$(MAKE) serverd
@$(MAKE) agentsdk
@$(MAKE) devicesdk
@$(MAKE) plugins
@$(MAKE) archive
@echo "$(COLOUR_GREEN)[OpenNHP] Build for platform ${OS_NAME} successfully done!$(END_COLOUR)"
Expand Down Expand Up @@ -68,6 +69,12 @@ ifeq ($(OS_NAME), linux)
gcc ./agent/sdkdemo/nhp-agent-demo.c -I ./release/nhp-agent -l:nhp-agent.so -L./release/nhp-agent -Wl,-rpath=. -o ./release/nhp-agent/nhp-agent-demo
endif

devicesdk:
ifeq ($(OS_NAME), linux)
go build -a -trimpath -buildmode=c-shared -ldflags ${LD_FLAGS} -v -o ./release/nhp-device/nhpdevice.so ./core/main/main.go ./core/main/nhpdevice.go
# gcc ./core/sdkdemo/nhp-device-demo.c -I ./release/nhp-device -I ./core/main -l:nhpdevice.so -L./release/nhp-device -Wl,-rpath=. -o ./release/nhp-device/nhp-device-demo
endif

plugins:
@if test -d $(NHP_PLUGINS); then $(MAKE) -C $(NHP_PLUGINS); fi

Expand All @@ -76,4 +83,4 @@ archive:
@cd release && mkdir -p archive && tar -czvf ./archive/$(PACKAGE_FILE) nhp-agent nhp-ac nhp-server
@echo "$(COLOUR_GREEN)[opennhp] Package ${PACKAGE_FILE} archived!$(END_COLOUR)"

.PHONY: all generate-version-and-build init agentsdk plugins archive
.PHONY: all generate-version-and-build init agentsdk devicesdk plugins archive
36 changes: 18 additions & 18 deletions ac/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"os"
"path/filepath"

"github.com/OpenNHP/opennhp/core"
"github.com/OpenNHP/opennhp/log"
"github.com/OpenNHP/opennhp/nhp"
"github.com/OpenNHP/opennhp/utils"

toml "github.com/pelletier/go-toml/v2"
Expand All @@ -21,21 +21,21 @@ var (
)

type Config struct {
PrivateKeyBase64 string `json:"privateKey"`
ACId string `json:"acId"`
DefaultIp string `json:"defaultIp"`
AuthServiceId string `json:"aspId"`
ResourceIds []string `json:"resIds"`
Servers []*nhp.UdpPeer `json:"servers"`
IpPassMode int `json:"ipPassMode"` // 0: pass the knock source IP, 1: use pre-access mode and release the access source IP
LogLevel int `json:"logLevel"`
PrivateKeyBase64 string `json:"privateKey"`
ACId string `json:"acId"`
DefaultIp string `json:"defaultIp"`
AuthServiceId string `json:"aspId"`
ResourceIds []string `json:"resIds"`
Servers []*core.UdpPeer `json:"servers"`
IpPassMode int `json:"ipPassMode"` // 0: pass the knock source IP, 1: use pre-access mode and release the access source IP
LogLevel int `json:"logLevel"`
}

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

func (d *UdpDoor) loadBaseConfig() error {
func (d *UdpAC) loadBaseConfig() error {
// config.toml
fileName := filepath.Join(ExeDirPath, "etc", "config.toml")
if err := d.updateBaseConfig(fileName); err != nil {
Expand All @@ -50,7 +50,7 @@ func (d *UdpDoor) loadBaseConfig() error {
return nil
}

func (d *UdpDoor) loadPeers() error {
func (d *UdpAC) loadPeers() error {
// server.toml
fileName := filepath.Join(ExeDirPath, "etc", "server.toml")
if err := d.updateServerPeers(fileName); err != nil {
Expand All @@ -66,7 +66,7 @@ func (d *UdpDoor) loadPeers() error {
return nil
}

func (d *UdpDoor) updateBaseConfig(file string) (err error) {
func (d *UdpAC) updateBaseConfig(file string) (err error) {
utils.CatchPanicThenRun(func() {
err = errLoadConfig
})
Expand Down Expand Up @@ -107,7 +107,7 @@ func (d *UdpDoor) updateBaseConfig(file string) (err error) {
return err
}

func (d *UdpDoor) updateServerPeers(file string) (err error) {
func (d *UdpAC) updateServerPeers(file string) (err error) {
utils.CatchPanicThenRun(func() {
err = errLoadConfig
})
Expand All @@ -119,12 +119,12 @@ func (d *UdpDoor) updateServerPeers(file string) (err error) {

// update
var peers Peers
serverPeerMap := make(map[string]*nhp.UdpPeer)
serverPeerMap := make(map[string]*core.UdpPeer)
if err := toml.Unmarshal(content, &peers); err != nil {
log.Error("failed to unmarshal server peer config: %v", err)
}
for _, p := range peers.Servers {
p.Type = nhp.NHP_SERVER
p.Type = core.NHP_SERVER
d.device.AddPeer(p)
serverPeerMap[p.PublicKeyBase64()] = p
}
Expand All @@ -142,11 +142,11 @@ func (d *UdpDoor) updateServerPeers(file string) (err error) {
return err
}

func (d *UdpDoor) IpPassMode() int {
func (d *UdpAC) IpPassMode() int {
return d.config.IpPassMode
}

func (d *UdpDoor) StopConfigWatch() {
func (d *UdpAC) StopConfigWatch() {
if baseConfigWatch != nil {
baseConfigWatch.Close()
}
Expand Down
14 changes: 7 additions & 7 deletions ac/main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
"syscall"

"github.com/OpenNHP/opennhp/ac"
"github.com/OpenNHP/opennhp/nhp"
"github.com/OpenNHP/opennhp/core"
"github.com/OpenNHP/opennhp/version"
"github.com/urfave/cli/v2"
)

func main() {
app := cli.NewApp()
app.Name = "nhp-ac"
app.Usage = "door entity for NHP protocol"
app.Usage = "ac entity for NHP protocol"
app.Version = version.Version

runCmd := &cli.Command{
Name: "run",
Usage: "create and run door process for NHP protocol",
Usage: "create and run ac process for NHP protocol",
Action: func(c *cli.Context) error {
return runApp()
},
Expand All @@ -35,11 +35,11 @@ func main() {
&cli.BoolFlag{Name: "sm2", Value: false, DisableDefaultText: true, Usage: "generate sm2 keys"},
},
Action: func(c *cli.Context) error {
var e nhp.Ecdh
var e core.Ecdh
if c.Bool("sm2") {
e = nhp.NewECDH(nhp.ECC_SM2)
e = core.NewECDH(core.ECC_SM2)
} else {
e = nhp.NewECDH(nhp.ECC_CURVE25519)
e = core.NewECDH(core.ECC_CURVE25519)
}
pub := e.PublicKeyBase64()
priv := e.PrivateKeyBase64()
Expand All @@ -66,7 +66,7 @@ func runApp() error {
}
exeDirPath := filepath.Dir(exeFilePath)

d := &ac.UdpDoor{}
d := &ac.UdpAC{}
err = d.Start(exeDirPath, 4)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 4925f96

Please sign in to comment.