2018-02-10 13:34:42 +01:00
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
2018-02-11 11:15:11 +01:00
|
|
|
"time"
|
2018-02-10 13:34:42 +01:00
|
|
|
|
|
|
|
"dev.sum7.eu/genofire/yaja/client"
|
|
|
|
"dev.sum7.eu/genofire/yaja/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Status struct {
|
|
|
|
client *client.Client
|
2018-02-10 16:45:01 +01:00
|
|
|
account *Account
|
2018-02-10 13:34:42 +01:00
|
|
|
JID *model.JID `json:"jid"`
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
Login bool `json:"is_online"`
|
|
|
|
MessageForConnection map[string]string `json:"-"`
|
|
|
|
Connections map[string]bool `json:"-"`
|
|
|
|
TLSVersion string `json:"tls_version"`
|
2018-02-11 11:15:11 +01:00
|
|
|
IPv4 bool `json:"ipv4"`
|
|
|
|
IPv6 bool `json:"ipv6"`
|
2018-02-10 13:34:42 +01:00
|
|
|
}
|
|
|
|
|
2018-02-10 16:45:01 +01:00
|
|
|
func NewStatus(acc *Account) *Status {
|
2018-02-10 13:34:42 +01:00
|
|
|
return &Status{
|
2018-02-10 16:45:01 +01:00
|
|
|
account: acc,
|
|
|
|
JID: acc.JID,
|
|
|
|
Domain: acc.JID.Domain,
|
2018-02-10 13:34:42 +01:00
|
|
|
MessageForConnection: make(map[string]string),
|
|
|
|
Connections: make(map[string]bool),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-11 11:15:11 +01:00
|
|
|
func (s *Status) Update(timeout time.Duration) {
|
2018-02-10 13:34:42 +01:00
|
|
|
if s.client == nil || !s.Login {
|
|
|
|
s.Login = false
|
|
|
|
s.TLSVersion = ""
|
|
|
|
return
|
|
|
|
}
|
2018-02-11 11:15:11 +01:00
|
|
|
|
2018-02-11 22:03:58 +01:00
|
|
|
c := &client.Client{
|
|
|
|
JID: model.NewJID(s.account.JID.Bare()),
|
|
|
|
Protocol: "tcp4",
|
|
|
|
Logging: s.client.Logging,
|
|
|
|
Timeout: timeout / 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.Connect(s.account.Password); err == nil {
|
2018-02-11 11:15:11 +01:00
|
|
|
s.IPv4 = true
|
2018-02-11 22:03:58 +01:00
|
|
|
c.Close()
|
2018-02-11 11:15:11 +01:00
|
|
|
} else {
|
|
|
|
s.IPv4 = false
|
|
|
|
}
|
2018-02-11 22:03:58 +01:00
|
|
|
|
|
|
|
c.JID = model.NewJID(s.account.JID.Bare())
|
|
|
|
c.Protocol = "tcp6"
|
|
|
|
|
|
|
|
if err := c.Connect(s.account.Password); err == nil {
|
2018-02-11 11:15:11 +01:00
|
|
|
s.IPv6 = true
|
2018-02-11 22:03:58 +01:00
|
|
|
c.Close()
|
2018-02-11 11:15:11 +01:00
|
|
|
} else {
|
|
|
|
s.IPv6 = false
|
|
|
|
}
|
|
|
|
if !s.IPv4 && !s.IPv6 {
|
|
|
|
s.client.Close()
|
|
|
|
s.Login = false
|
|
|
|
s.TLSVersion = ""
|
|
|
|
}
|
|
|
|
|
2018-02-10 13:34:42 +01:00
|
|
|
if tlsstate := s.client.TLSConnectionState(); tlsstate != nil {
|
|
|
|
switch tlsstate.Version {
|
|
|
|
case tls.VersionSSL30:
|
|
|
|
s.TLSVersion = "SSL 3.0"
|
|
|
|
case tls.VersionTLS10:
|
|
|
|
s.TLSVersion = "TLS 1.0"
|
|
|
|
case tls.VersionTLS11:
|
|
|
|
s.TLSVersion = "TLS 1.1"
|
|
|
|
case tls.VersionTLS12:
|
|
|
|
s.TLSVersion = "TLS 1.2"
|
|
|
|
default:
|
|
|
|
s.TLSVersion = "unknown " + string(tlsstate.Version)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
s.TLSVersion = ""
|
|
|
|
}
|
|
|
|
}
|