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
.glide/
tmp

View File

@ -43,7 +43,7 @@ var serverCmd = &cobra.Command{
log.Fatal("unable to load config file:", err)
}
log.SetLevel(log.DebugLevel)
log.SetLevel(configData.Logging.Level)
err = file.ReadJSON(configData.StatePath, db)
if err != nil {
@ -61,17 +61,26 @@ var serverCmd = &cobra.Command{
}
// https server to handle acme (by letsencrypt)
httpServer := &http.Server{
Addr: ":https",
for _, addr := range configData.Address.Webserver {
hs := &http.Server{
Addr: addr,
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{
TLSManager: &m,
Database: db,
ClientAddr: configData.Address.Client,
ServerAddr: configData.Address.Server,
LoggingClient: configData.Logging.LevelClient,
RegisterEnable: configData.Register.Enable,
RegisterDomains: configData.Register.Domains,
}
go statesaveWorker.Start()
@ -115,6 +124,10 @@ func reload() {
log.Warn("unable to load config file:", err)
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)
@ -139,16 +152,18 @@ func reload() {
certs = &tls.Config{GetCertificate: m.GetCertificate}
restartServer = true
}
if restartServer {
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,
}
if restartServer {
go srv.Start()
log.Warn("reloading need a restart:")
go newServer.Start()
//TODO should fetch new server error
srv.Close()
srv = newServer

View File

@ -1,6 +1,15 @@
tlsdir = "/tmp/ssl"
state_path = "/tmp/yaja.json"
tlsdir = "tmp/ssl"
state_path = "tmp/yaja.json"
[logging]
level = 6
level_client = 6
[register]
enable = true
domains = []
[address]
webserver = [":https"]
client = [":5222"]
server = [":5269"]

View File

@ -1,9 +1,22 @@
package config
import (
log "github.com/sirupsen/logrus"
)
type Config struct {
TLSDir string `toml:"tlsdir"`
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 {
Webserver []string `toml:"webserver"`
Client []string `toml:"client"`
Server []string `toml:"server"`
} `toml:"address"`

View File

@ -25,7 +25,7 @@ type Client struct {
func NewClient(conn net.Conn, srv *Server) *Client {
logger := log.New()
logger.SetLevel(log.DebugLevel)
logger.SetLevel(srv.LoggingClient)
client := &Client{
Conn: conn,
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() {
client.close <- true
client.Conn.Close()

View File

@ -15,6 +15,9 @@ type Server struct {
ClientAddr []string
ServerAddr []string
Database *database.State
LoggingClient log.Level
RegisterEnable bool `toml:"enable"`
RegisterDomains []string `toml:"domains"`
}
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'>`,
createCookie(), messages.NSClient, messages.NSStream)
if client.DomainRegisterAllowed() {
fmt.Fprintf(client.Conn, `<stream:features>
<mechanisms xmlns='%s'>
<mechanism>PLAIN</mechanism>
@ -157,6 +158,14 @@ func (state *TLSStream) Process(client *Client) (State, *Client) {
<register xmlns='%s'/>
</stream:features>`,
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
}

View File

@ -19,6 +19,11 @@ func (state *RegisterFormRequest) Process(client *Client) (State, *Client) {
client.log.Debug("running")
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
if err := client.in.DecodeElement(&msg, state.element); err != nil {
client.log.Warn("is no iq: ", err)
@ -65,6 +70,11 @@ func (state *RegisterRequest) Process(client *Client) (State, *Client) {
client.log.Debug("running")
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()
if err != nil {
client.log.Warn("unable to read: ", err)