move webserver, logging and registraion to config
This commit is contained in:
parent
800a5b1917
commit
a079961c8b
|
@ -14,3 +14,4 @@
|
||||||
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
|
||||||
.glide/
|
.glide/
|
||||||
|
|
||||||
|
tmp
|
||||||
|
|
|
@ -43,7 +43,7 @@ var serverCmd = &cobra.Command{
|
||||||
log.Fatal("unable to load config file:", err)
|
log.Fatal("unable to load config file:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(configData.Logging.Level)
|
||||||
|
|
||||||
err = file.ReadJSON(configData.StatePath, db)
|
err = file.ReadJSON(configData.StatePath, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,17 +61,26 @@ var serverCmd = &cobra.Command{
|
||||||
}
|
}
|
||||||
|
|
||||||
// https server to handle acme (by letsencrypt)
|
// https server to handle acme (by letsencrypt)
|
||||||
httpServer := &http.Server{
|
for _, addr := range configData.Address.Webserver {
|
||||||
Addr: ":https",
|
hs := &http.Server{
|
||||||
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
|
Addr: addr,
|
||||||
|
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
|
||||||
|
}
|
||||||
|
go func(hs *http.Server, addr string) {
|
||||||
|
if err := hs.ListenAndServeTLS("", ""); err != http.ErrServerClosed {
|
||||||
|
log.Errorf("webserver with addr %s: %s", addr, err)
|
||||||
|
}
|
||||||
|
}(hs, addr)
|
||||||
}
|
}
|
||||||
go httpServer.ListenAndServeTLS("", "")
|
|
||||||
|
|
||||||
srv = &server.Server{
|
srv = &server.Server{
|
||||||
TLSManager: &m,
|
TLSManager: &m,
|
||||||
Database: db,
|
Database: db,
|
||||||
ClientAddr: configData.Address.Client,
|
ClientAddr: configData.Address.Client,
|
||||||
ServerAddr: configData.Address.Server,
|
ServerAddr: configData.Address.Server,
|
||||||
|
LoggingClient: configData.Logging.LevelClient,
|
||||||
|
RegisterEnable: configData.Register.Enable,
|
||||||
|
RegisterDomains: configData.Register.Domains,
|
||||||
}
|
}
|
||||||
|
|
||||||
go statesaveWorker.Start()
|
go statesaveWorker.Start()
|
||||||
|
@ -115,6 +124,10 @@ func reload() {
|
||||||
log.Warn("unable to load config file:", err)
|
log.Warn("unable to load config file:", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
log.SetLevel(configNewData.Logging.Level)
|
||||||
|
srv.LoggingClient = configNewData.Logging.LevelClient
|
||||||
|
srv.RegisterEnable = configNewData.Register.Enable
|
||||||
|
srv.RegisterDomains = configNewData.Register.Domains
|
||||||
|
|
||||||
//TODO fetch changing address (to set restart)
|
//TODO fetch changing address (to set restart)
|
||||||
|
|
||||||
|
@ -139,16 +152,18 @@ func reload() {
|
||||||
certs = &tls.Config{GetCertificate: m.GetCertificate}
|
certs = &tls.Config{GetCertificate: m.GetCertificate}
|
||||||
restartServer = true
|
restartServer = true
|
||||||
}
|
}
|
||||||
|
|
||||||
newServer := &server.Server{
|
|
||||||
TLSConfig: certs,
|
|
||||||
Database: db,
|
|
||||||
ClientAddr: configNewData.Address.Client,
|
|
||||||
ServerAddr: configNewData.Address.Server,
|
|
||||||
}
|
|
||||||
|
|
||||||
if restartServer {
|
if restartServer {
|
||||||
go srv.Start()
|
newServer := &server.Server{
|
||||||
|
TLSConfig: certs,
|
||||||
|
Database: db,
|
||||||
|
ClientAddr: configNewData.Address.Client,
|
||||||
|
ServerAddr: configNewData.Address.Server,
|
||||||
|
LoggingClient: configNewData.Logging.LevelClient,
|
||||||
|
RegisterEnable: configNewData.Register.Enable,
|
||||||
|
RegisterDomains: configNewData.Register.Domains,
|
||||||
|
}
|
||||||
|
log.Warn("reloading need a restart:")
|
||||||
|
go newServer.Start()
|
||||||
//TODO should fetch new server error
|
//TODO should fetch new server error
|
||||||
srv.Close()
|
srv.Close()
|
||||||
srv = newServer
|
srv = newServer
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
tlsdir = "/tmp/ssl"
|
tlsdir = "tmp/ssl"
|
||||||
state_path = "/tmp/yaja.json"
|
state_path = "tmp/yaja.json"
|
||||||
|
|
||||||
|
[logging]
|
||||||
|
level = 6
|
||||||
|
level_client = 6
|
||||||
|
|
||||||
|
[register]
|
||||||
|
enable = true
|
||||||
|
domains = []
|
||||||
|
|
||||||
[address]
|
[address]
|
||||||
|
webserver = [":https"]
|
||||||
client = [":5222"]
|
client = [":5222"]
|
||||||
server = [":5269"]
|
server = [":5269"]
|
||||||
|
|
|
@ -1,10 +1,23 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
TLSDir string `toml:"tlsdir"`
|
TLSDir string `toml:"tlsdir"`
|
||||||
StatePath string `toml:"state_path"`
|
StatePath string `toml:"state_path"`
|
||||||
Address struct {
|
Logging struct {
|
||||||
Client []string `toml:"client"`
|
Level log.Level `toml:"level"`
|
||||||
Server []string `toml:"server"`
|
LevelClient log.Level `toml:"level_client"`
|
||||||
|
} `toml:"logging"`
|
||||||
|
Register struct {
|
||||||
|
Enable bool `toml:"enable"`
|
||||||
|
Domains []string `toml:"domains"`
|
||||||
|
} `toml:"register"`
|
||||||
|
Address struct {
|
||||||
|
Webserver []string `toml:"webserver"`
|
||||||
|
Client []string `toml:"client"`
|
||||||
|
Server []string `toml:"server"`
|
||||||
} `toml:"address"`
|
} `toml:"address"`
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ type Client struct {
|
||||||
|
|
||||||
func NewClient(conn net.Conn, srv *Server) *Client {
|
func NewClient(conn net.Conn, srv *Server) *Client {
|
||||||
logger := log.New()
|
logger := log.New()
|
||||||
logger.SetLevel(log.DebugLevel)
|
logger.SetLevel(srv.LoggingClient)
|
||||||
client := &Client{
|
client := &Client{
|
||||||
Conn: conn,
|
Conn: conn,
|
||||||
Server: srv,
|
Server: srv,
|
||||||
|
@ -56,6 +56,20 @@ func (client *Client) Read() (*xml.StartElement, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *Client) DomainRegisterAllowed() bool {
|
||||||
|
if client.jid.Domain == "" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, domain := range client.Server.RegisterDomains {
|
||||||
|
if domain == client.jid.Domain {
|
||||||
|
|
||||||
|
return !client.Server.RegisterEnable
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return client.Server.RegisterEnable
|
||||||
|
}
|
||||||
|
|
||||||
func (client *Client) Close() {
|
func (client *Client) Close() {
|
||||||
client.close <- true
|
client.close <- true
|
||||||
client.Conn.Close()
|
client.Conn.Close()
|
||||||
|
|
|
@ -10,11 +10,14 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
TLSConfig *tls.Config
|
TLSConfig *tls.Config
|
||||||
TLSManager *autocert.Manager
|
TLSManager *autocert.Manager
|
||||||
ClientAddr []string
|
ClientAddr []string
|
||||||
ServerAddr []string
|
ServerAddr []string
|
||||||
Database *database.State
|
Database *database.State
|
||||||
|
LoggingClient log.Level
|
||||||
|
RegisterEnable bool `toml:"enable"`
|
||||||
|
RegisterDomains []string `toml:"domains"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) Start() {
|
func (srv *Server) Start() {
|
||||||
|
|
|
@ -150,13 +150,22 @@ func (state *TLSStream) Process(client *Client) (State, *Client) {
|
||||||
<stream:stream id='%x' version='1.0' xmlns='%s' xmlns:stream='%s'>`,
|
<stream:stream id='%x' version='1.0' xmlns='%s' xmlns:stream='%s'>`,
|
||||||
createCookie(), messages.NSClient, messages.NSStream)
|
createCookie(), messages.NSClient, messages.NSStream)
|
||||||
|
|
||||||
fmt.Fprintf(client.Conn, `<stream:features>
|
if client.DomainRegisterAllowed() {
|
||||||
|
fmt.Fprintf(client.Conn, `<stream:features>
|
||||||
<mechanisms xmlns='%s'>
|
<mechanisms xmlns='%s'>
|
||||||
<mechanism>PLAIN</mechanism>
|
<mechanism>PLAIN</mechanism>
|
||||||
</mechanisms>
|
</mechanisms>
|
||||||
<register xmlns='%s'/>
|
<register xmlns='%s'/>
|
||||||
</stream:features>`,
|
</stream:features>`,
|
||||||
messages.NSSASL, messages.NSFeaturesIQRegister)
|
messages.NSSASL, messages.NSFeaturesIQRegister)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(client.Conn, `<stream:features>
|
||||||
|
<mechanisms xmlns='%s'>
|
||||||
|
<mechanism>PLAIN</mechanism>
|
||||||
|
</mechanisms>
|
||||||
|
</stream:features>`,
|
||||||
|
messages.NSSASL)
|
||||||
|
}
|
||||||
|
|
||||||
return state.Next, client
|
return state.Next, client
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,11 @@ func (state *RegisterFormRequest) Process(client *Client) (State, *Client) {
|
||||||
client.log.Debug("running")
|
client.log.Debug("running")
|
||||||
defer client.log.Debug("leave")
|
defer client.log.Debug("leave")
|
||||||
|
|
||||||
|
if !client.DomainRegisterAllowed() {
|
||||||
|
client.log.Error("unpossible to reach this state, register on this domain is not allowed")
|
||||||
|
return nil, client
|
||||||
|
}
|
||||||
|
|
||||||
var msg messages.IQ
|
var msg messages.IQ
|
||||||
if err := client.in.DecodeElement(&msg, state.element); err != nil {
|
if err := client.in.DecodeElement(&msg, state.element); err != nil {
|
||||||
client.log.Warn("is no iq: ", err)
|
client.log.Warn("is no iq: ", err)
|
||||||
|
@ -65,6 +70,11 @@ func (state *RegisterRequest) Process(client *Client) (State, *Client) {
|
||||||
client.log.Debug("running")
|
client.log.Debug("running")
|
||||||
defer client.log.Debug("leave")
|
defer client.log.Debug("leave")
|
||||||
|
|
||||||
|
if !client.DomainRegisterAllowed() {
|
||||||
|
client.log.Error("unpossible to reach this state, register on this domain is not allowed")
|
||||||
|
return nil, client
|
||||||
|
}
|
||||||
|
|
||||||
element, err := client.Read()
|
element, err := client.Read()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
client.log.Warn("unable to read: ", err)
|
client.log.Warn("unable to read: ", err)
|
||||||
|
|
Reference in New Issue