Added GetTime. Added independent internet port. Debugged internet connection.
This commit is contained in:
@ -8,125 +8,147 @@ import (
|
||||
"strings"
|
||||
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/utils"
|
||||
)
|
||||
|
||||
var apns = map[string]string{
|
||||
"Tinkoff": "m.tinkoff",
|
||||
}
|
||||
|
||||
const pppConfigName = "annalistnet"
|
||||
const pppConfig = `
|
||||
# Annalist project custom internet connection
|
||||
|
||||
# APN:
|
||||
connect "/usr/sbin/chat -v -f /etc/chatscripts/gprs -T %s"
|
||||
|
||||
# Port:
|
||||
%s
|
||||
|
||||
# Baudrate:
|
||||
%d
|
||||
|
||||
noipdefault
|
||||
usepeerdns
|
||||
defaultroute
|
||||
persist
|
||||
noauth
|
||||
nocrtscts
|
||||
local
|
||||
`
|
||||
|
||||
type conn struct {
|
||||
logger *log.Logger
|
||||
port at.Port
|
||||
|
||||
pppPort string
|
||||
|
||||
isConnectExecuted bool
|
||||
isInited bool
|
||||
}
|
||||
|
||||
type Conn interface {
|
||||
Init() error
|
||||
Init(pppPort string) error
|
||||
|
||||
Connect() error
|
||||
Disconnect() error
|
||||
Ping() bool // Is connected
|
||||
|
||||
IsConnected() bool // Check interface existance
|
||||
Ping() error
|
||||
|
||||
io.Closer
|
||||
}
|
||||
|
||||
func New(logger *log.Logger, port at.Port) Conn {
|
||||
return &conn{
|
||||
logger: logger,
|
||||
port: port,
|
||||
logger: logger,
|
||||
port: port,
|
||||
isConnectExecuted: false,
|
||||
isInited: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *conn) Connect() error {
|
||||
if ok, err := utils.CheckService(c.port, c.logger); err != nil || !ok {
|
||||
if err != nil {
|
||||
return fmt.Errorf("check service: %w", err)
|
||||
}
|
||||
return fmt.Errorf("no service")
|
||||
func (c *conn) Init(pppPort string) error {
|
||||
c.pppPort = pppPort
|
||||
// Setup only setup
|
||||
if err := c.setup(); err != nil {
|
||||
return fmt.Errorf("setup: %w", err)
|
||||
}
|
||||
c.isInited = true
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Connect() error {
|
||||
if !c.isInited {
|
||||
return fmt.Errorf("internet submodule is not inited")
|
||||
}
|
||||
// Check is already connected
|
||||
if c.isConnectExecuted {
|
||||
return fmt.Errorf("already connected")
|
||||
}
|
||||
// Check signal
|
||||
// if ok, err := utils.CheckService(c.port, c.logger); err != nil || !ok {
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("check service: %w", err)
|
||||
// }
|
||||
// return fmt.Errorf("no service")
|
||||
// }
|
||||
|
||||
resp, err := exec.Command("pon", pppConfigName).Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute connect cmd: %w", err)
|
||||
return fmt.Errorf("execute pon cmd: %w", err)
|
||||
}
|
||||
c.logger.Println("DEBUG pon response:", string(resp))
|
||||
if len(resp) > 0 {
|
||||
c.logger.Println("pon response:", string(resp))
|
||||
}
|
||||
c.isConnectExecuted = true
|
||||
|
||||
// Set default route
|
||||
|
||||
c.logger.Println("\x1b[38;2;0;255;0mInternet connected.\x1b[38;2;255;255;255m")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Disconnect() error {
|
||||
// return nil // Temporary do not turn off inet
|
||||
if !c.isConnectExecuted {
|
||||
return nil
|
||||
// return fmt.Errorf("internet is not connected")
|
||||
}
|
||||
c.isConnectExecuted = false
|
||||
resp, err := exec.Command("poff", pppConfigName).Output()
|
||||
if err != nil {
|
||||
return fmt.Errorf("execute disconnect cmd: %w", err)
|
||||
return fmt.Errorf("execute poff cmd: %w", err)
|
||||
}
|
||||
c.logger.Println("DEBUG poff response:", string(resp))
|
||||
if len(resp) > 0 {
|
||||
c.logger.Println("poff response:", string(resp))
|
||||
}
|
||||
c.isConnectExecuted = false
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Init() error {
|
||||
// Setup
|
||||
if err := c.setup(); err != nil {
|
||||
return fmt.Errorf("setup: %w", err)
|
||||
}
|
||||
// // Connect
|
||||
// c.logger.Println("Connect...")
|
||||
// if err := c.Connect(); err != nil {
|
||||
// return fmt.Errorf("connect: %w", err)
|
||||
// }
|
||||
|
||||
// //DEBUG
|
||||
// resp, err := exec.Command("ifconfig").Output()
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("execute ifconfig cmd: %w", err)
|
||||
// }
|
||||
// c.logger.Println("DEBUG ifconfig resp:", string(resp))
|
||||
|
||||
// // Test connectin using Ping
|
||||
// c.logger.Println("Test...")
|
||||
// if !c.Ping() {
|
||||
// return fmt.Errorf("ping failed")
|
||||
// }
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) Ping() bool {
|
||||
func (c *conn) Ping() error {
|
||||
// Test - try to connect to Google DNS
|
||||
// ping -I ppp0 8.8.8.8
|
||||
resp, err := exec.Command("ping", "8.8.8.8").Output()
|
||||
if err != nil {
|
||||
c.logger.Println("Ping 1 cmd error:", err)
|
||||
c.logger.Println("Ping default interface cmd error:", err)
|
||||
}
|
||||
c.logger.Println("Ping 1 resp:", string(resp))
|
||||
c.logger.Println("Ping default interface resp:", string(resp))
|
||||
|
||||
resp, err = exec.Command("ping", "-I", "ppp0", "8.8.8.8").Output()
|
||||
if err != nil {
|
||||
c.logger.Println("Ping 2 cmd error:", err)
|
||||
c.logger.Println("Ping ppp0 interface cmd error:", err)
|
||||
}
|
||||
c.logger.Println("Ping 2 resp:", string(resp))
|
||||
c.logger.Println("Ping ppp0 interface resp:", string(resp))
|
||||
|
||||
return !(strings.Contains(string(resp), "Destination Host Unreachable") || strings.Contains(string(resp), "Destination Net Unreachable")) // tmp solution
|
||||
if strings.Contains(string(resp), "Destination Host Unreachable") || strings.Contains(string(resp), "Destination Net Unreachable") {
|
||||
return fmt.Errorf("ping response: %s", string(resp))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *conn) IsConnected() bool {
|
||||
if !c.isConnectExecuted {
|
||||
return false
|
||||
}
|
||||
// Make "ifconfig" request
|
||||
resp, err := exec.Command("ifconfig").Output()
|
||||
if err != nil {
|
||||
c.logger.Println("ifconfig cmd error:", err.Error())
|
||||
return false
|
||||
}
|
||||
lines := strings.Split(string(resp), "\n")
|
||||
for _, l := range lines {
|
||||
if len(l) == 0 {
|
||||
continue
|
||||
}
|
||||
if l[0] == ' ' {
|
||||
continue
|
||||
}
|
||||
interfaceName := strings.Split(l, ":")[0]
|
||||
if interfaceName == "ppp0" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false // Did not found
|
||||
}
|
||||
|
||||
func (c *conn) Close() error {
|
||||
c.isInited = false
|
||||
if err := c.Disconnect(); err != nil {
|
||||
return fmt.Errorf("diconnect: %w", err)
|
||||
}
|
||||
|
@ -9,27 +9,40 @@ import (
|
||||
"gitea.unprism.ru/KRBL/sim-modem/api/modem/utils"
|
||||
)
|
||||
|
||||
var apns = map[string]string{
|
||||
"Tinkoff": "m.tinkoff",
|
||||
"Megafon": "internet",
|
||||
"BeeLine": "internet.beeline.ru",
|
||||
}
|
||||
|
||||
const pppConfigName = "annalistnet"
|
||||
const pppConfig = `
|
||||
# Annalist project custom internet connection
|
||||
|
||||
# APN:
|
||||
connect "/usr/sbin/chat -v -f /etc/chatscripts/gprs -T %s"
|
||||
|
||||
# Port:
|
||||
%s
|
||||
|
||||
# Baudrate:
|
||||
%d
|
||||
|
||||
noipdefault
|
||||
usepeerdns
|
||||
defaultroute
|
||||
persist
|
||||
noauth
|
||||
nocrtscts
|
||||
local
|
||||
`
|
||||
|
||||
func (c *conn) setup() error {
|
||||
c.logger.Println("Check requirenments...")
|
||||
if err := c.checkReqs(); err != nil {
|
||||
return fmt.Errorf("check requirenments: %w", err)
|
||||
}
|
||||
// DEBUG show networks and signal
|
||||
resp, err := c.port.Send("AT+COPS?")
|
||||
if err != nil {
|
||||
return fmt.Errorf("AT+COPS? request: %w", err)
|
||||
}
|
||||
c.logger.Println("DEBUG networks:", resp)
|
||||
|
||||
resp, err = c.port.Send("AT+CSQ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("AT+CSQ request: %w", err)
|
||||
}
|
||||
c.logger.Println("DEBUG signal quality:", resp)
|
||||
|
||||
// Configure ppp
|
||||
// what is better ASK If /etc/ppp/peers/annalistnet not exists
|
||||
c.logger.Println("Configure ppp...")
|
||||
if err := c.configurePPP(); err != nil {
|
||||
return fmt.Errorf("configure ppp: %w", err)
|
||||
}
|
||||
@ -75,9 +88,8 @@ func (c *conn) ensurePackage(pname string) error {
|
||||
|
||||
func (c *conn) checkPackageExist(pname string) bool {
|
||||
resp, err := exec.Command("apt-mark", "showmanual", pname).Output()
|
||||
c.logger.Println("CHECK:", resp)
|
||||
if err != nil {
|
||||
c.logger.Println("CHECK PACKAGE ERROR: ", err.Error())
|
||||
c.logger.Println("check package error: ", err.Error())
|
||||
return false
|
||||
}
|
||||
return string(resp[:len(pname)]) == pname
|
||||
@ -103,8 +115,8 @@ func (c *conn) configurePPP() error {
|
||||
}
|
||||
|
||||
// Make config
|
||||
c.logger.Printf("Config values: %s, %s, %d", apn, c.port.GetName(), c.port.GetBaudrate())
|
||||
config := fmt.Sprintf(pppConfig, apn, c.port.GetName(), c.port.GetBaudrate())
|
||||
c.logger.Printf("Config ppp values: %s, %s, %d", apn, c.pppPort, c.port.GetBaudrate())
|
||||
config := fmt.Sprintf(pppConfig, apn, c.pppPort, c.port.GetBaudrate())
|
||||
|
||||
// Write to file
|
||||
f, err := os.OpenFile("/etc/ppp/peers/"+pppConfigName, os.O_CREATE|os.O_WRONLY, 0666)
|
||||
|
Reference in New Issue
Block a user