2024-07-27 13:01:27 +00:00
|
|
|
package internet
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2024-08-08 10:32:55 +00:00
|
|
|
"gitea.unprism.ru/KRBL/sim-modem/api/modem/at"
|
2024-08-12 10:40:26 +00:00
|
|
|
"gitea.unprism.ru/KRBL/sim-modem/api/modem/utils"
|
2024-07-27 13:01:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var apns = map[string]string{
|
2024-07-29 12:17:00 +00:00
|
|
|
"Tinkoff": "m.tinkoff",
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type Conn interface {
|
|
|
|
Init() error
|
2024-08-12 10:40:26 +00:00
|
|
|
Connect() error
|
|
|
|
Disconnect() error
|
2024-07-27 13:01:27 +00:00
|
|
|
Ping() bool // Is connected
|
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(logger *log.Logger, port at.Port) Conn {
|
|
|
|
return &conn{
|
|
|
|
logger: logger,
|
|
|
|
port: port,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
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")
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
2024-07-29 11:02:06 +00:00
|
|
|
resp, err := exec.Command("pon", pppConfigName).Output()
|
2024-07-27 13:01:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("execute connect cmd: %w", err)
|
|
|
|
}
|
2024-07-29 12:17:00 +00:00
|
|
|
c.logger.Println("DEBUG pon response:", string(resp))
|
2024-07-27 13:01:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
func (c *conn) Disconnect() error {
|
2024-07-29 11:02:06 +00:00
|
|
|
resp, err := exec.Command("poff", pppConfigName).Output()
|
2024-07-27 13:01:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("execute disconnect cmd: %w", err)
|
|
|
|
}
|
2024-07-29 12:17:00 +00:00
|
|
|
c.logger.Println("DEBUG poff response:", string(resp))
|
2024-07-27 13:01:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Init() error {
|
|
|
|
// Setup
|
|
|
|
if err := c.setup(); err != nil {
|
|
|
|
return fmt.Errorf("setup: %w", err)
|
|
|
|
}
|
2024-08-12 10:40:26 +00:00
|
|
|
// // Connect
|
|
|
|
// c.logger.Println("Connect...")
|
|
|
|
// if err := c.Connect(); err != nil {
|
|
|
|
// return fmt.Errorf("connect: %w", err)
|
|
|
|
// }
|
2024-07-27 13:01:27 +00:00
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
// //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))
|
2024-07-27 13:01:27 +00:00
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
// // Test connectin using Ping
|
|
|
|
// c.logger.Println("Test...")
|
|
|
|
// if !c.Ping() {
|
|
|
|
// return fmt.Errorf("ping failed")
|
|
|
|
// }
|
2024-07-27 13:01:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Ping() bool {
|
|
|
|
// Test - try to connect to Google DNS
|
|
|
|
// ping -I ppp0 8.8.8.8
|
2024-07-29 12:17:00 +00:00
|
|
|
resp, err := exec.Command("ping", "8.8.8.8").Output()
|
2024-07-27 13:01:27 +00:00
|
|
|
if err != nil {
|
2024-07-29 12:17:00 +00:00
|
|
|
c.logger.Println("Ping 1 cmd error:", err)
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
2024-07-29 12:17:00 +00:00
|
|
|
c.logger.Println("Ping 1 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 2 resp:", string(resp))
|
|
|
|
|
2024-08-02 16:16:39 +00:00
|
|
|
return !(strings.Contains(string(resp), "Destination Host Unreachable") || strings.Contains(string(resp), "Destination Net Unreachable")) // tmp solution
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Close() error {
|
2024-08-12 10:40:26 +00:00
|
|
|
if err := c.Disconnect(); err != nil {
|
2024-07-27 13:01:27 +00:00
|
|
|
return fmt.Errorf("diconnect: %w", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|