Skip to content

Commit

Permalink
feat(dns): support use multiple root domains (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
Li4n0 committed Jan 7, 2022
1 parent fc505b0 commit 913e0f7
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 41 deletions.
2 changes: 1 addition & 1 deletion CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RevSuit will generate a profile template the first time it is run, and you can e
version: 1.3
addr: :10000 # Address of the HTTP service will listen
token: # Authentication Token, both the admin page and the client need to be authenticated by this Token
domain: # The domain name used by the platform
domains: [] # The domain names used by the platform
external_ip: # The external IP of the platform, you need to make sure that the target you want to test can access the platform through this IP
admin_path_prefix: "/revsuit" # The http path prefix for the admin page, the page will be located at: /admin_path_prefix/admin
database: revsuit.db # Database connection information, support using Sqlite3, MySQL, Postgres
Expand Down
2 changes: 1 addition & 1 deletion CONFIG.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
version: 1.3
addr: :10000 # HTTP 服务监听的地址
token: # 鉴权Token,管理页面和客户端都需要通过该 Token 进行鉴权
domain: # 反连平台绑定的域名
domains: [] # 反连平台绑定的域名
external_ip: # 反连平台的外部IP,需要确保你想测试的目标能通过该 IP 访问到平台
admin_path_prefix: "/revsuit" # 管理页面的 http path 前缀,管理页面将位于:/admin_path_prefix/admin
database: revsuit.db # 数据库连接信息 支持Sqlite3、MySQL、Postgres
Expand Down
4 changes: 2 additions & 2 deletions internal/cli/config.tpl.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
version: 1.4
version: 1.5
addr: :10000
token:
domain:
domains: []
external_ip: # You need to make sure that your target can access the server through this ip.
admin_path_prefix: "/revsuit"
database: revsuit.db
Expand Down
61 changes: 32 additions & 29 deletions pkg/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import (

type Server struct {
Config
serverDomain string
serverIP string
rules []*Rule
rulesLock sync.RWMutex
livingLock sync.Mutex
serverDomains []string
serverIP string
rules []*Rule
rulesLock sync.RWMutex
livingLock sync.Mutex
}

var (
Expand All @@ -37,8 +37,8 @@ func GetServer() *Server {
return server
}

func (s *Server) SetServerDomain(serverDomain string) *Server {
s.serverDomain = serverDomain
func (s *Server) SetServerDomain(serverDomains []string) *Server {
s.serverDomains = serverDomains
return s
}

Expand Down Expand Up @@ -217,39 +217,42 @@ func (s *Server) Run() {
// create server
server := newdns.NewServer(newdns.Config{
Handler: func(name string) (*newdns.Zone, error) {
if name == s.serverDomain+"." {
return &newdns.Zone{
Name: getZoneName(s.serverDomain),
MasterNameServer: "ns1.hostmaster.com.",
AllNameServers: []string{
"ns1.hostmaster.com.",
"ns2.hostmaster.com.",
"ns3.hostmaster.com.",
},
Handler: func(_, remoteAddr string) ([]newdns.Set, error) {
return []newdns.Set{
{
Name: name,
Type: newdns.A,
TTL: 10,
Records: []newdns.Record{
{
Address: s.serverIP,
},
}}}, nil
}}, nil
for _, serverDomain := range s.serverDomains {
if name == serverDomain+"." {
return &newdns.Zone{
Name: getZoneName(serverDomain),
MasterNameServer: "ns1.hostmaster.com.",
AllNameServers: []string{
"ns1.hostmaster.com.",
"ns2.hostmaster.com.",
"ns3.hostmaster.com.",
},
Handler: func(_, remoteAddr string) ([]newdns.Set, error) {
return []newdns.Set{
{
Name: name,
Type: newdns.A,
TTL: 10,
Records: []newdns.Record{
{
Address: s.serverIP,
},
}}}, nil
}}, nil
}
}
return s.newZone(name), nil
},
})

// run server
log.Info("Starting DNS Server at :53, resolve %s to %s", s.serverDomain, s.serverIP)
log.Info("Starting DNS Server at :53, resolve %v to %s", s.serverDomains, s.serverIP)
go func() {
s.livingLock.Lock()
if !s.Enable {
server.Close()
}
s.livingLock.Unlock()
}()

err := server.Run(s.Addr)
Expand Down
1 change: 1 addition & 0 deletions pkg/ftp/ftp.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ func (s *Server) Run() {
if !s.Enable {
_ = listener.Close()
}
s.livingLock.Unlock()
}()

for {
Expand Down
1 change: 1 addition & 0 deletions pkg/ldap/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ func (s *Server) Run() {
if !s.Enable {
_ = listener.Close()
}
s.livingLock.Unlock()
}()

for {
Expand Down
1 change: 1 addition & 0 deletions pkg/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ func (s *Server) Run() {
if !s.Enable {
s.listener.Close()
}
s.livingLock.Unlock()
}()

s.listener.Accept()
Expand Down
1 change: 1 addition & 0 deletions pkg/rmi/rmi.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ func (s *Server) Run() {
if !s.Enable {
_ = listener.Close()
}
s.livingLock.Unlock()
}()

for {
Expand Down
2 changes: 1 addition & 1 deletion pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Config struct {
Version float64
Addr string
Token string
Domain string
Domains []string
ExternalIP string `yaml:"external_ip"`
AdminPathPrefix string `yaml:"admin_path_prefix"`
Database string
Expand Down
4 changes: 2 additions & 2 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ func New(c *Config) *Revsuit {
if c.HTTP.IpHeader != "" {
s.http.SetIpHeader(c.HTTP.IpHeader)
}
if c.Domain != "" {
s.dns.SetServerDomain(c.Domain)
if len(c.Domains) != 0 {
s.dns.SetServerDomain(c.Domains)
}
if c.ExternalIP != "" {
s.dns.SetServerIP(c.ExternalIP)
Expand Down
21 changes: 16 additions & 5 deletions pkg/server/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package server
import (
"fmt"
"io"
"strings"
"time"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -166,7 +167,7 @@ func (revsuit *Revsuit) getPlatformConfig(c *gin.Context) {
var res = make(map[string]string)
res["Addr"] = revsuit.config.Addr
res["Token"] = revsuit.config.Token
res["Domain"] = revsuit.config.Domain
res["Domains"] = strings.Join(revsuit.config.Domains, ",")
res["AdminPathPrefix"] = revsuit.config.AdminPathPrefix
res["ExternalIP"] = revsuit.config.ExternalIP
res["Database"] = revsuit.config.Database
Expand Down Expand Up @@ -215,10 +216,10 @@ func (revsuit *Revsuit) updatePlatformConfig(c *gin.Context) {
log.Info("Update http config [ip_header] to %s", form["IpHeader"])
}

if form["Domain"] != revsuit.config.Domain {
revsuit.config.Domain = form["Domain"]
revsuit.dns.SetServerDomain(form["Domain"])
log.Info("Update platform config [domain] to %s", form["Domain"])
if form["Domains"] != strings.Join(revsuit.config.Domains, ",") {
revsuit.config.Domains = strings.Split(form["Domains"], ",")
revsuit.dns.SetServerDomain(revsuit.config.Domains)
log.Info("Update platform config [domain] to %v", revsuit.config.Domains)
if revsuit.dns.Enable {
revsuit.dns.Restart()
}
Expand Down Expand Up @@ -307,6 +308,11 @@ func (revsuit *Revsuit) updateDnsConfig(c *gin.Context) {
return
}

if form.Addr != revsuit.dns.Addr {
revsuit.dns.Addr = form.Addr
log.Info("Update dns config [addr] to %s", form.Addr)
}

if form.Enable != revsuit.dns.Enable {
log.Info("Update dns config [enable] to %v", form.Enable)
if form.Enable {
Expand All @@ -316,6 +322,11 @@ func (revsuit *Revsuit) updateDnsConfig(c *gin.Context) {
}
return
}

if revsuit.dns.Enable {
revsuit.dns.Restart()
}

}

func (revsuit *Revsuit) getMySQLConfig(c *gin.Context) {
Expand Down

0 comments on commit 913e0f7

Please sign in to comment.