sum7
/
yaja
Archived
1
0
Fork 0

move webserver, logging and registraion to config

This commit is contained in:
Martin Geno 2017-12-15 22:24:42 +01:00
parent 800a5b1917
commit a079961c8b
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
8 changed files with 105 additions and 31 deletions

1
.gitignore vendored
View File

@ -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

View File

@ -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{
Addr: addr,
TLSConfig: &tls.Config{GetCertificate: m.GetCertificate}, TLSConfig: &tls.Config{GetCertificate: m.GetCertificate},
} }
go httpServer.ListenAndServeTLS("", "") 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)
}
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
} }
if restartServer {
newServer := &server.Server{ newServer := &server.Server{
TLSConfig: certs, TLSConfig: certs,
Database: db, Database: db,
ClientAddr: configNewData.Address.Client, ClientAddr: configNewData.Address.Client,
ServerAddr: configNewData.Address.Server, ServerAddr: configNewData.Address.Server,
LoggingClient: configNewData.Logging.LevelClient,
RegisterEnable: configNewData.Register.Enable,
RegisterDomains: configNewData.Register.Domains,
} }
log.Warn("reloading need a restart:")
if restartServer { go newServer.Start()
go srv.Start()
//TODO should fetch new server error //TODO should fetch new server error
srv.Close() srv.Close()
srv = newServer srv = newServer

View File

@ -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"]

View File

@ -1,9 +1,22 @@
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"`
Logging struct {
Level log.Level `toml:"level"`
LevelClient log.Level `toml:"level_client"`
} `toml:"logging"`
Register struct {
Enable bool `toml:"enable"`
Domains []string `toml:"domains"`
} `toml:"register"`
Address struct { Address struct {
Webserver []string `toml:"webserver"`
Client []string `toml:"client"` Client []string `toml:"client"`
Server []string `toml:"server"` Server []string `toml:"server"`
} `toml:"address"` } `toml:"address"`

View File

@ -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()

View File

@ -15,6 +15,9 @@ type Server struct {
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() {

View File

@ -150,6 +150,7 @@ 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)
if client.DomainRegisterAllowed() {
fmt.Fprintf(client.Conn, `<stream:features> fmt.Fprintf(client.Conn, `<stream:features>
<mechanisms xmlns='%s'> <mechanisms xmlns='%s'>
<mechanism>PLAIN</mechanism> <mechanism>PLAIN</mechanism>
@ -157,6 +158,14 @@ func (state *TLSStream) Process(client *Client) (State, *Client) {
<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
} }

View File

@ -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)