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-07-27 13:01:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type conn struct {
|
|
|
|
logger *log.Logger
|
|
|
|
port at.Port
|
2024-08-12 16:24:31 +00:00
|
|
|
|
|
|
|
pppPort string
|
|
|
|
|
|
|
|
isConnectExecuted bool
|
|
|
|
isInited bool
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Conn interface {
|
2024-08-12 16:24:31 +00:00
|
|
|
Init(pppPort string) error
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
Connect() error
|
|
|
|
Disconnect() error
|
2024-08-12 16:24:31 +00:00
|
|
|
|
|
|
|
IsConnected() bool // Check interface existance
|
|
|
|
Ping() error
|
|
|
|
|
2024-07-27 13:01:27 +00:00
|
|
|
io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(logger *log.Logger, port at.Port) Conn {
|
|
|
|
return &conn{
|
2024-08-12 16:24:31 +00:00
|
|
|
logger: logger,
|
|
|
|
port: port,
|
|
|
|
isConnectExecuted: false,
|
|
|
|
isInited: false,
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:24:31 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
func (c *conn) Connect() error {
|
2024-08-12 16:24:31 +00:00
|
|
|
if !c.isInited {
|
|
|
|
return fmt.Errorf("internet submodule is not inited")
|
|
|
|
}
|
|
|
|
// Check is already connected
|
|
|
|
if c.isConnectExecuted {
|
|
|
|
return fmt.Errorf("already connected")
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
2024-08-12 16:24:31 +00:00
|
|
|
// 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")
|
|
|
|
// }
|
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 {
|
2024-08-12 16:24:31 +00:00
|
|
|
return fmt.Errorf("execute pon cmd: %w", err)
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
2024-08-12 16:24:31 +00:00
|
|
|
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")
|
2024-07-27 13:01:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 10:40:26 +00:00
|
|
|
func (c *conn) Disconnect() error {
|
2024-08-12 16:24:31 +00:00
|
|
|
// return nil // Temporary do not turn off inet
|
|
|
|
if !c.isConnectExecuted {
|
|
|
|
return nil
|
|
|
|
// return fmt.Errorf("internet is not connected")
|
|
|
|
}
|
|
|
|
c.isConnectExecuted = false
|
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 {
|
2024-08-12 16:24:31 +00:00
|
|
|
return fmt.Errorf("execute poff cmd: %w", err)
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
2024-08-12 16:24:31 +00:00
|
|
|
if len(resp) > 0 {
|
|
|
|
c.logger.Println("poff response:", string(resp))
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
2024-08-12 16:24:31 +00:00
|
|
|
c.isConnectExecuted = false
|
2024-07-27 13:01:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-12 16:24:31 +00:00
|
|
|
func (c *conn) Ping() error {
|
2024-07-27 13:01:27 +00:00
|
|
|
// 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-08-12 16:24:31 +00:00
|
|
|
c.logger.Println("Ping default interface cmd error:", err)
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
2024-08-12 16:24:31 +00:00
|
|
|
c.logger.Println("Ping default interface resp:", string(resp))
|
2024-07-29 12:17:00 +00:00
|
|
|
|
|
|
|
resp, err = exec.Command("ping", "-I", "ppp0", "8.8.8.8").Output()
|
|
|
|
if err != nil {
|
2024-08-12 16:24:31 +00:00
|
|
|
c.logger.Println("Ping ppp0 interface cmd error:", err)
|
2024-07-29 12:17:00 +00:00
|
|
|
}
|
2024-08-12 16:24:31 +00:00
|
|
|
c.logger.Println("Ping ppp0 interface resp:", string(resp))
|
2024-07-29 12:17:00 +00:00
|
|
|
|
2024-08-12 16:24:31 +00:00
|
|
|
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
|
2024-07-27 13:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *conn) Close() error {
|
2024-08-12 16:24:31 +00:00
|
|
|
c.isInited = false
|
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
|
|
|
|
}
|