mirror of
https://github.com/Li4n0/revsuit.git
synced 2026-09-25 08:01:54 +08:00
feat: add domain and external ip configuration items (#18)
This commit is contained in:
+55
-11
@@ -17,9 +17,11 @@ import (
|
||||
|
||||
type Server struct {
|
||||
Config
|
||||
rules []*Rule
|
||||
rulesLock sync.RWMutex
|
||||
livingLock sync.Mutex
|
||||
serverDomain string
|
||||
serverIP string
|
||||
rules []*Rule
|
||||
rulesLock sync.RWMutex
|
||||
livingLock sync.Mutex
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -35,6 +37,16 @@ func GetServer() *Server {
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) SetServerDomain(serverDomain string) *Server {
|
||||
s.serverDomain = serverDomain
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) SetServerIP(serverIP string) *Server {
|
||||
s.serverIP = serverIP
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) getRules() []*Rule {
|
||||
defer s.rulesLock.RUnlock()
|
||||
s.rulesLock.RLock()
|
||||
@@ -53,6 +65,7 @@ func newSet(_rule *Rule, name, value, ip string, _type newdns.Type) []newdns.Set
|
||||
{
|
||||
Name: name,
|
||||
Type: _type,
|
||||
TTL: _rule.TTL * time.Second,
|
||||
Records: func() []newdns.Record {
|
||||
switch _rule.Type {
|
||||
case newdns.TXT:
|
||||
@@ -82,12 +95,20 @@ func newSet(_rule *Rule, name, value, ip string, _type newdns.Type) []newdns.Set
|
||||
return []newdns.Record{{Address: value}}
|
||||
}
|
||||
}(),
|
||||
TTL: _rule.TTL * time.Second,
|
||||
},
|
||||
}
|
||||
return set
|
||||
}
|
||||
|
||||
func getZoneName(domain string) string {
|
||||
frags := strings.Split(domain, ".")
|
||||
zoneName := domain
|
||||
if len(frags) >= 2 {
|
||||
zoneName = strings.Join(frags[len(frags)-2:], ".") + "."
|
||||
}
|
||||
return zoneName
|
||||
}
|
||||
|
||||
// newZone creates new dns zone with root domain
|
||||
func (s *Server) newZone(name string) *newdns.Zone {
|
||||
defer func() {
|
||||
@@ -97,13 +118,8 @@ func (s *Server) newZone(name string) *newdns.Zone {
|
||||
}()
|
||||
|
||||
domain := strings.TrimSuffix(name, ".")
|
||||
frags := strings.Split(domain, ".")
|
||||
zoneName := name
|
||||
if len(frags) >= 2 {
|
||||
zoneName = strings.Join(frags[len(frags)-2:], ".") + "."
|
||||
}
|
||||
zone := &newdns.Zone{
|
||||
Name: zoneName,
|
||||
Name: getZoneName(domain),
|
||||
MasterNameServer: "ns1.hostmaster.com.",
|
||||
AllNameServers: []string{
|
||||
"ns1.hostmaster.com.",
|
||||
@@ -175,6 +191,12 @@ func (s *Server) Stop() {
|
||||
s.livingLock.Unlock()
|
||||
}
|
||||
|
||||
func (s *Server) Restart() {
|
||||
s.Stop()
|
||||
time.Sleep(time.Second * 2)
|
||||
go s.Run()
|
||||
}
|
||||
|
||||
func (s *Server) Run() {
|
||||
s.Enable = true
|
||||
s.livingLock.Lock()
|
||||
@@ -195,12 +217,34 @@ 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
|
||||
}
|
||||
return s.newZone(name), nil
|
||||
},
|
||||
})
|
||||
|
||||
// run server
|
||||
log.Info("Starting DNS Server at :53")
|
||||
log.Info("Starting DNS Server at :53, resolve %s to %s", s.serverDomain, s.serverIP)
|
||||
go func() {
|
||||
s.livingLock.Lock()
|
||||
if !s.Enable {
|
||||
|
||||
+1
-2
@@ -3,6 +3,5 @@ package ftp
|
||||
type Config struct {
|
||||
Enable bool
|
||||
Addr string
|
||||
PasvIP string `yaml:"pasv_ip"`
|
||||
PasvPort int `yaml:"pasv_port"`
|
||||
PasvPort int `yaml:"pasv_port"`
|
||||
}
|
||||
|
||||
+9
-3
@@ -22,6 +22,7 @@ import (
|
||||
|
||||
type Server struct {
|
||||
Config
|
||||
pasvIP string
|
||||
rules []*Rule
|
||||
rulesLock sync.RWMutex
|
||||
livingLock sync.Mutex
|
||||
@@ -55,6 +56,11 @@ func GetServer() *Server {
|
||||
return server
|
||||
}
|
||||
|
||||
func (s *Server) SetPasvIP(ip string) *Server {
|
||||
s.pasvIP = ip
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) getRules() []*Rule {
|
||||
defer s.rulesLock.RUnlock()
|
||||
s.rulesLock.RLock()
|
||||
@@ -194,10 +200,10 @@ loop:
|
||||
_, _ = connBuf.WriteString(UserLogged)
|
||||
|
||||
if pasvAddress = s.getPasvAddressFromCache(ip, _rule.PasvAddress); pasvAddress == "" {
|
||||
pasvAddress = fmt.Sprintf("%s:%d", s.PasvIP, s.PasvPort)
|
||||
pasvAddress = fmt.Sprintf("%s:%d", s.pasvIP, s.PasvPort)
|
||||
}
|
||||
|
||||
isRedirect = rule.CompileTpl(pasvAddress, vars) != fmt.Sprintf("%s:%d", s.PasvIP, s.PasvPort)
|
||||
isRedirect = rule.CompileTpl(pasvAddress, vars) != fmt.Sprintf("%s:%d", s.pasvIP, s.PasvPort)
|
||||
case "SIZE":
|
||||
path += strings.TrimLeft(args, "/")
|
||||
if _rule == nil || isRedirect || len(_rule.Data) == 0 {
|
||||
@@ -312,7 +318,7 @@ func (s *Server) handlePasvConnection(conn net.Conn, data map[string]interface{}
|
||||
// run pasv server
|
||||
func (s *Server) runPasvServer() (net.Listener, error) {
|
||||
pasvAddress := fmt.Sprintf("%s:%d", strings.Split(s.Addr, ":")[0], s.PasvPort)
|
||||
log.Info("Start to listen FTP PASV port at %v, PasvIP is %v", pasvAddress, s.PasvIP)
|
||||
log.Info("Start to listen FTP PASV port at %v, PasvIP is %v", pasvAddress, s.pasvIP)
|
||||
listener, err := net.Listen("tcp", pasvAddress)
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -16,11 +16,14 @@ type noticeConfig struct {
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Addr string
|
||||
Token string
|
||||
Database string
|
||||
LogLevel string `yaml:"log_level"`
|
||||
Notice noticeConfig
|
||||
Version float64
|
||||
Addr string
|
||||
Token string
|
||||
Domain string
|
||||
ExternalIP string `yaml:"external_ip"`
|
||||
Database string
|
||||
LogLevel string `yaml:"log_level"`
|
||||
Notice noticeConfig
|
||||
rhttp.Config
|
||||
DNS dns.Config
|
||||
MySQL mysql.Config
|
||||
|
||||
@@ -59,8 +59,8 @@ func (revsuit *Revsuit) registerHttpRouter() {
|
||||
settingsGroup := revsuit.http.ApiGroup.Group("setting")
|
||||
settingsGroup.GET("/exportRules", exportRules)
|
||||
settingsGroup.POST("/importRules", importRules)
|
||||
settingsGroup.GET("/getHttpConfig", revsuit.getHttpConfig)
|
||||
settingsGroup.POST("/updateHttpConfig", revsuit.updateHttpConfig)
|
||||
settingsGroup.GET("/getPlatformConfig", revsuit.getPlatformConfig)
|
||||
settingsGroup.POST("/updatePlatformConfig", revsuit.updatePlatformConfig)
|
||||
settingsGroup.GET("/getDnsConfig", revsuit.getDnsConfig)
|
||||
settingsGroup.POST("/updateDnsConfig", revsuit.updateDnsConfig)
|
||||
settingsGroup.GET("/getRmiConfig", revsuit.getRmiConfig)
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
log "unknwon.dev/clog/v2"
|
||||
)
|
||||
|
||||
const VERSION = "0.1.1-beta"
|
||||
const VERSION = "0.1.2-beta"
|
||||
|
||||
type Revsuit struct {
|
||||
config *Config
|
||||
@@ -181,6 +181,13 @@ func New(c *Config) *Revsuit {
|
||||
if c.IpHeader != "" {
|
||||
s.http.SetIpHeader(c.IpHeader)
|
||||
}
|
||||
if c.Domain != "" {
|
||||
s.dns.SetServerDomain(c.Domain)
|
||||
}
|
||||
if c.ExternalIP != "" {
|
||||
s.dns.SetServerIP(c.ExternalIP)
|
||||
s.ftp.SetPasvIP(c.ExternalIP)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
|
||||
+32
-7
@@ -138,10 +138,12 @@ func importRules(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
func (revsuit *Revsuit) getHttpConfig(c *gin.Context) {
|
||||
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["ExternalIP"] = revsuit.config.ExternalIP
|
||||
res["Database"] = revsuit.config.Database
|
||||
res["LogLevel"] = revsuit.config.LogLevel
|
||||
res["IpHeader"] = revsuit.config.IpHeader
|
||||
@@ -149,7 +151,7 @@ func (revsuit *Revsuit) getHttpConfig(c *gin.Context) {
|
||||
c.JSON(200, res)
|
||||
}
|
||||
|
||||
func (revsuit *Revsuit) updateHttpConfig(c *gin.Context) {
|
||||
func (revsuit *Revsuit) updatePlatformConfig(c *gin.Context) {
|
||||
var form = make(map[string]string)
|
||||
|
||||
if err := c.ShouldBindJSON(&form); err != nil {
|
||||
@@ -188,6 +190,34 @@ func (revsuit *Revsuit) updateHttpConfig(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 revsuit.dns.Enable {
|
||||
revsuit.dns.Restart()
|
||||
}
|
||||
}
|
||||
|
||||
if form["ExternalIP"] != revsuit.config.ExternalIP {
|
||||
revsuit.config.ExternalIP = form["ExternalIP"]
|
||||
revsuit.dns.SetServerIP(form["ExternalIP"])
|
||||
revsuit.ftp.SetPasvIP(form["ExternalIP"])
|
||||
log.Info("Update platform config [ExternalIP] to %s", form["ExternalIP"])
|
||||
if revsuit.dns.Enable {
|
||||
revsuit.dns.Restart()
|
||||
}
|
||||
if revsuit.ftp.Enable {
|
||||
revsuit.ftp.Restart()
|
||||
}
|
||||
}
|
||||
|
||||
if form["Token"] != revsuit.config.Token {
|
||||
revsuit.config.Token = form["Token"]
|
||||
revsuit.http.SetToken(form["Token"])
|
||||
log.Info("Update platform config [token] to %s", form["Token"])
|
||||
}
|
||||
|
||||
c.JSON(200, gin.H{
|
||||
"status": "succeed",
|
||||
"error": nil,
|
||||
@@ -216,11 +246,6 @@ func (revsuit *Revsuit) updateFtpConfig(c *gin.Context) {
|
||||
log.Info("Update ftp config [addr] to %s", form.Addr)
|
||||
}
|
||||
|
||||
if form.PasvIP != revsuit.ftp.PasvIP {
|
||||
revsuit.ftp.PasvIP = form.PasvIP
|
||||
log.Info("Update ftp config [pasv_ip] to %s", form.PasvIP)
|
||||
}
|
||||
|
||||
if form.PasvPort != revsuit.ftp.PasvPort {
|
||||
revsuit.ftp.PasvPort = form.PasvPort
|
||||
log.Info("Update ftp config [pasv_port] to %d", form.PasvPort)
|
||||
|
||||
Reference in New Issue
Block a user