Skip to content

Commit 067f021

Browse files
committed
20230504
1 parent 4593ad1 commit 067f021

File tree

26 files changed

+209
-135
lines changed

26 files changed

+209
-135
lines changed

README.md

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ Uranus is a Linux firewalld central controller. In Greek mythology, Uranus king
66

77
[Click](#Screenshot)
88

9+
![](./images/Uranus.png)
10+
911
## Features
10-
- Full firewalld features (currently converted OS debian11, centos7)
11-
- Full D-BUS API convert to REST API.
12+
- Full firewalld features
13+
- Full D-BUS API convert to REST API.(currently converted OS debian11, centos7)
1214
- Based dbus remotely.
13-
- HTTP restful API.
1415
- Declarative API and Imperative API.
1516
- Asynchronous batch interface (only add).
1617
- Can control thousands of linux machine via firewall gateway remotely.
@@ -22,6 +23,7 @@ Uranus is a Linux firewalld central controller. In Greek mythology, Uranus king
2223
- Only HTTP Service (without store).
2324
- UI based VUE-element-admin.
2425
- Support datacenter tag and machine management.
26+
- Support SQLite & MySQL databases.
2527

2628
## TODO
2729
- [X] Asynchronous batch process
@@ -33,7 +35,6 @@ Uranus is a Linux firewalld central controller. In Greek mythology, Uranus king
3335
- [X] Deplyment on Kubernetes & Docker
3436
- [ ] Prometheus Metics.
3537
- [ ] WAF SDK.
36-
- [ ] Token destory
3738

3839

3940
## Deploy
@@ -59,14 +60,6 @@ docker run -d --rm cylonchau/uranus
5960

6061
if you think update you dbus-daemon verion to lasest, can use `dbus.spec` make your package.
6162

62-
63-
## Thanks libs
64-
- [kubernetes workqueue](https://github.com/kubernetes/kubernetes)
65-
- [klog](https://github.com/kubernetes/kubernetes)
66-
- [godbus](https://github.com/godbus/dbus)
67-
- [gin](https://github.com/gin-gonic/gin)
68-
- [viper](https://github.com/spf13/viper)
69-
7063
## use
7164

7265
[HTTP API DOC](https://documenter.getpostman.com/view/12796679/UV5agGNr)

images/Uranus.png

27.4 KB
Loading

server/apis/code.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ var (
3232
ErrZoneNotFount = &Errno{Code: 40004, Message: "Not found the zone"}
3333
ErrForwardNotFount = &Errno{Code: 40004, Message: "The Forward in the zone is empty"}
3434

35-
// auther errors
35+
// token errors
3636
ErrEncrypt = &Errno{Code: 50101, Message: "success"}
3737
ErrUserNotFound = &Errno{Code: 50102, Message: "User not found"}
3838
ErrTokenInvalid = &Errno{Code: 50103, Message: "Invalied token"}
3939
ErrPasswordIncorrect = &Errno{Code: 50104, Message: "Incorrect username or password"}
4040
ErrUserExist = &Errno{Code: 50105, Message: "User exists"}
4141
ErrUserNotExist = &Errno{Code: 50106, Message: "User does not exist"}
4242
ErrNeedAuth = &Errno{Code: 50107, Message: "Your need authetication"}
43+
ErrTokenDestoryed = &Errno{Code: 50108, Message: "Token is destoryed"}
4344
ErrSendSMSTooMany = &Errno{Code: 50109, Message: "已超出当日限制,请明天再试"}
4445
ErrVerifyCode = &Errno{Code: 50110, Message: "验证码错误"}
4546
ErrEmailOrPassword = &Errno{Code: 50111, Message: "邮箱或密码错误"}

server/apis/firewalld_response.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,20 @@ type ResponseSlice struct {
1818
Data interface{} `form:"data" json:"data,omitempty"`
1919
}
2020

21-
func AuthFailed(ctx *gin.Context, msg *Errno) {
21+
func AuthFailed(ctx *gin.Context, msg *Errno, data interface{}) {
2222
ctx.JSON(http.StatusUnauthorized, Response{
2323
Code: msg.Code,
2424
Msg: msg.Message,
25+
Data: data,
2526
})
2627
}
2728

2829
// API403Response ....
29-
func Auth403Failed(ctx *gin.Context, msg *Errno) {
30+
func Auth403Failed(ctx *gin.Context, msg *Errno, data interface{}) {
3031
ctx.JSON(http.StatusForbidden, Response{
3132
Code: msg.Code,
3233
Msg: msg.Message,
34+
Data: data,
3335
})
3436
}
3537

server/apis/user_request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@ type InfoQuery struct {
1818
}
1919

2020
type InfoResp struct {
21-
Username string `form:"token" json:"token" binding:"required"`
22-
UserRole string `form:"token" json:"token" binding:"required"`
21+
Username string `form:"username" json:"username" binding:"required"`
22+
UserRole string `form:"role" json:"role" binding:"required"`
2323
}

server/app/auth/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"reflect"
55

66
"github.com/cylonchau/firewalld-gateway/server/apis"
7-
"github.com/cylonchau/firewalld-gateway/server/app/auther"
7+
token2 "github.com/cylonchau/firewalld-gateway/utils/auther"
88
userModel "github.com/cylonchau/firewalld-gateway/utils/model"
99

1010
"github.com/gin-gonic/gin"
@@ -46,7 +46,7 @@ func (u *Auth) signinHandler(c *gin.Context) {
4646
var ip uint32
4747
if ip, enconterError = userModel.GetRequestIP(c.Request); enconterError == nil {
4848
userModel.LastLogin(int64(user.ID), ip)
49-
if token, enconterError = auther.GenToken(int64(user.ID)); enconterError == nil {
49+
if token, enconterError = token2.GenToken(int64(user.ID)); enconterError == nil {
5050
apis.SuccessResponse(c, nil, apis.UserResp{
5151
UserID: uint64(user.ID),
5252
Token: token,
@@ -56,7 +56,7 @@ func (u *Auth) signinHandler(c *gin.Context) {
5656
}
5757
}
5858

59-
if token, enconterError = auther.GenToken(int64(user.ID)); enconterError == nil {
59+
if token, enconterError = token2.GenToken(int64(user.ID)); enconterError == nil {
6060
apis.SuccessResponse(c, nil, apis.UserResp{
6161
UserID: uint64(user.ID),
6262
Token: token,
@@ -98,7 +98,7 @@ func (u *Auth) userInfoHandler(c *gin.Context) {
9898
return
9999
}
100100

101-
uid, err := auther.GetInfo(userInfoQuery.Token)
101+
uid, err := token2.GetInfo(userInfoQuery.Token)
102102
if err != nil {
103103
apis.APIResponse(c, err, nil)
104104
return
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package v1
2+
3+
import (
4+
"github.com/gin-gonic/gin"
5+
6+
code "github.com/cylonchau/firewalld-gateway/server/apis"
7+
"github.com/cylonchau/firewalld-gateway/utils/firewalld"
8+
)
9+
10+
type DashboardRouter struct{}
11+
12+
func (this *DashboardRouter) RegisterPortAPI(g *gin.RouterGroup) {
13+
dashboardGroup := g.Group("/dashboard")
14+
dashboardGroup.GET("/", this.getRuntimeStatus)
15+
16+
}
17+
18+
// getRuntimeStatus ...
19+
// @Summary getRuntimeStatus
20+
// @Produce json
21+
// @Success 200 {object} internal.Response
22+
// @Router /fw/v1/dashboard [GET]
23+
func (this *DashboardRouter) getRuntimeStatus(c *gin.Context) {
24+
25+
var query = &code.Query{}
26+
err := c.BindQuery(query)
27+
28+
if err != nil {
29+
code.APIResponse(c, err, nil)
30+
return
31+
}
32+
33+
dbusClient, err := firewalld.NewDbusClientService(query.Ip)
34+
if err != nil {
35+
code.ConnectDbusService(c, err)
36+
return
37+
}
38+
defer dbusClient.Destroy()
39+
defaultPolicy := dbusClient.GetDefaultPolicy()
40+
defaultZone := dbusClient.GetDefaultZone()
41+
var richCount, portCount, serviceCount int
42+
var natStatus bool
43+
44+
if richs, err := dbusClient.GetRichRules(defaultZone); err == nil {
45+
richCount = len(richs)
46+
if ports, err := dbusClient.GetPorts(defaultZone); err == nil {
47+
portCount = len(ports)
48+
if services, err := dbusClient.GetServices(); err == nil {
49+
serviceCount = len(services)
50+
if b, err := dbusClient.QueryMasquerade(defaultZone); err == nil {
51+
natStatus = b
52+
}
53+
}
54+
}
55+
}
56+
57+
if err == nil {
58+
status := make(map[string]interface{})
59+
status["default_zone"] = defaultZone
60+
status["default_policy"] = defaultPolicy
61+
status["nat_status"] = natStatus
62+
status["rich"] = richCount
63+
status["port"] = portCount
64+
status["service"] = serviceCount
65+
code.SuccessResponse(c, code.OK, status)
66+
} else {
67+
code.SuccessResponse(c, code.OK, err)
68+
}
69+
70+
}

server/app/firewalld/v1/masquerade.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import (
1010
type MasqueradeRouter struct{}
1111

1212
func (this *MasqueradeRouter) RegisterPortAPI(g *gin.RouterGroup) {
13-
portGroup := g.Group("/masquerade")
14-
15-
portGroup.PUT("/", this.enableInRuntime)
16-
portGroup.DELETE("/", this.disableInRuntime)
17-
portGroup.GET("/", this.queryInRuntime)
18-
portGroup.PUT("/permanent", this.enableInPermanent)
19-
portGroup.DELETE("/permanent", this.disableInPermanent)
20-
portGroup.GET("/query", this.queryInPermanent)
13+
masqueradeGroup := g.Group("/masquerade")
14+
15+
masqueradeGroup.PUT("/", this.enableInRuntime)
16+
masqueradeGroup.DELETE("/", this.disableInRuntime)
17+
masqueradeGroup.GET("/", this.queryInRuntime)
18+
masqueradeGroup.PUT("/permanent", this.enableInPermanent)
19+
masqueradeGroup.DELETE("/permanent", this.disableInPermanent)
20+
masqueradeGroup.GET("/query", this.queryInPermanent)
2121
}
2222

2323
// enableInRuntime ...

server/app/firewalld/v1/nat.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ import (
1212
type NATRouter struct{}
1313

1414
func (this *NATRouter) RegisterNATRouterAPI(g *gin.RouterGroup) {
15-
portGroup := g.Group("/nat")
15+
natGroup := g.Group("/nat")
1616

17-
portGroup.POST("/", this.addForwardInRuntime)
18-
portGroup.GET("/", this.getForwardInRuntime)
19-
portGroup.DELETE("/", this.delForwardInRuntime)
17+
natGroup.POST("/", this.addForwardInRuntime)
18+
natGroup.GET("/", this.getForwardInRuntime)
19+
natGroup.DELETE("/", this.delForwardInRuntime)
2020
}
2121

2222
// addForward ...

server/app/firewalld/v1/rich_rule.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import (
1010
type RichRuleRouter struct{}
1111

1212
func (this *RichRuleRouter) RegisterPortAPI(g *gin.RouterGroup) {
13-
portGroup := g.Group("/rich")
14-
portGroup.POST("/", this.addRichRuleAtRuntime)
15-
portGroup.GET("/", this.getRichRulesAtRuntime)
16-
portGroup.DELETE("/", this.delRichRuleAtRuntime)
13+
richGroup := g.Group("/rich")
14+
richGroup.POST("/", this.addRichRuleAtRuntime)
15+
richGroup.GET("/", this.getRichRulesAtRuntime)
16+
richGroup.DELETE("/", this.delRichRuleAtRuntime)
1717
}
1818

1919
// GetRichRules ...

0 commit comments

Comments
 (0)