|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "bytes" |
| 6 | + "context" |
| 7 | + "encoding/hex" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "strconv" |
| 11 | + "strings" |
| 12 | + |
| 13 | + "github.com/Tomy2e/livebox-api-client" |
| 14 | + "github.com/Tomy2e/livebox-api-client/api/request" |
| 15 | +) |
| 16 | + |
| 17 | +// NewClient creates a new Livebox API client. |
| 18 | +func NewClient(ip, password string) (*livebox.Client, error) { |
| 19 | + client, err := livebox.NewClient(password, livebox.WithAddress("http://"+ip)) |
| 20 | + if err != nil { |
| 21 | + return nil, fmt.Errorf("failed to create client: %w", err) |
| 22 | + } |
| 23 | + return client, nil |
| 24 | +} |
| 25 | + |
| 26 | +// GetFunboxValues fetches values from a Funbox. |
| 27 | +func GetFunboxValues(client *livebox.Client) (*FunboxValues, error) { |
| 28 | + var fbvalues FunboxValues |
| 29 | + if err := client.Request(context.Background(), request.New("NeMo.Intf.data", "getMIBs", nil), &fbvalues); err != nil { |
| 30 | + return nil, fmt.Errorf("could not get funbox values: %w", err) |
| 31 | + } |
| 32 | + return &fbvalues, nil |
| 33 | +} |
| 34 | + |
| 35 | +// GetOntInfos fetches ONT information from the Livebox. |
| 36 | +func GetOntInfos(client *livebox.Client) (*Ont, error) { |
| 37 | + var ont Ont |
| 38 | + if err := client.Request(context.Background(), request.New("NeMo.Intf.veip0", "getMIBs", map[string]interface{}{"mibs": "gpon"}), &ont); err != nil { |
| 39 | + return nil, fmt.Errorf("could not get ONT infos: %w", err) |
| 40 | + } |
| 41 | + return &ont, nil |
| 42 | +} |
| 43 | + |
| 44 | +// GetMacAddress fetches the MAC address of the Livebox. |
| 45 | +func GetMacAddress(client *livebox.Client) (string, error) { |
| 46 | + var mac MacAddress |
| 47 | + if err := client.Request(context.Background(), request.New("NMC", "getWANStatus", nil), &mac); err != nil { |
| 48 | + return "", fmt.Errorf("could not get MAC address: %w", err) |
| 49 | + } |
| 50 | + return mac.Data.MACAddress, nil |
| 51 | +} |
| 52 | + |
| 53 | +// GetInternetVlan fetches the VLAN ID for the internet connection. |
| 54 | +func GetInternetVlan(client *livebox.Client) (int, error) { |
| 55 | + var vlaninternet VlanInternet |
| 56 | + if err := client.Request(context.Background(), request.New("NeMo.Intf.data", "getFirstParameter", map[string]interface{}{"name": "VLANID"}), &vlaninternet); err != nil { |
| 57 | + return 0, fmt.Errorf("could not get internet vlan: %w", err) |
| 58 | + } |
| 59 | + return vlaninternet.Status, nil |
| 60 | +} |
| 61 | + |
| 62 | +// GetDHCPInfos fetches DHCP information from the Livebox. |
| 63 | +func GetDHCPInfos(client *livebox.Client) (*DHCPInfo, error) { |
| 64 | + var dhcpinfos DHCP |
| 65 | + if err := client.Request(context.Background(), request.New("NeMo.Intf.data", "getMIBs", map[string]interface{}{"mibs": "dhcp"}), &dhcpinfos); err != nil { |
| 66 | + return nil, fmt.Errorf("could not get DHCP infos: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + option60 := dhcpinfos.Status.Dhcp.DhcpData.SentOption.Num60.Value |
| 70 | + option60decoded, err := hex.DecodeString(option60) |
| 71 | + if err != nil { |
| 72 | + return nil, fmt.Errorf("could not decode option 60: %w", err) |
| 73 | + } |
| 74 | + |
| 75 | + option70 := dhcpinfos.Status.Dhcp.DhcpData.SentOption.Num77.Value |
| 76 | + option70decoded, err := hex.DecodeString(option70) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("could not decode option 70: %w", err) |
| 79 | + } |
| 80 | + |
| 81 | + option70decodedstring := string(option70decoded) |
| 82 | + |
| 83 | + opt70 := strings.Split(option70decodedstring, "+") |
| 84 | + |
| 85 | + var dhcpoption77 string |
| 86 | + if len(opt70) > 1 { |
| 87 | + dhcpoption77 = opt70[1] |
| 88 | + } else { |
| 89 | + dhcpoption77 = option70decodedstring |
| 90 | + } |
| 91 | + |
| 92 | + // Add : every 2 char for DHCP Option 90 |
| 93 | + var buffer bytes.Buffer |
| 94 | + var n1 = 2 - 1 |
| 95 | + var l1 = len(dhcpinfos.Status.Dhcp.DhcpData.SentOption.Num90.Value) - 1 |
| 96 | + for i, runner := range dhcpinfos.Status.Dhcp.DhcpData.SentOption.Num90.Value { |
| 97 | + buffer.WriteRune(runner) |
| 98 | + if i%2 == n1 && i != l1 { |
| 99 | + buffer.WriteRune(':') |
| 100 | + } |
| 101 | + } |
| 102 | + dhcpoption90 := buffer.String() |
| 103 | + |
| 104 | + return &DHCPInfo{ |
| 105 | + Option60: string(option60decoded), |
| 106 | + Option77: dhcpoption77, |
| 107 | + Option90: dhcpoption90, |
| 108 | + }, nil |
| 109 | +} |
| 110 | + |
| 111 | +var oltVendorIDMap = map[string]string{ |
| 112 | + "HWTC": "136", |
| 113 | + "ALCL": "128", |
| 114 | + "130": "130", // Placeholder for your OLT Vendor ID |
| 115 | +} |
| 116 | + |
| 117 | +func generateOMCC(oltvendorid, hwHwver, omciSwVer1, omciSwVer2 string) { |
| 118 | + id, ok := oltVendorIDMap[oltvendorid] |
| 119 | + if !ok { |
| 120 | + id = "128" // default value |
| 121 | + fmt.Println("Unknown OLT VENDOR ID, defaulting to 128") |
| 122 | + } |
| 123 | + |
| 124 | + fmt.Println("Execute this command -> flash set OMCC_VER " + id) |
| 125 | + |
| 126 | + if oltvendorid == "ALCL" { |
| 127 | + if len(hwHwver) != 0 { |
| 128 | + fmt.Println("flash set HW_HWVER " + hwHwver) |
| 129 | + } |
| 130 | + fmt.Println("flash set OMCI_SW_VER1 " + omciSwVer1) |
| 131 | + fmt.Println("flash set OMCI_SW_VER2 " + omciSwVer2) |
| 132 | + fmt.Println("flash set OMCI_TM_OPT 0") |
| 133 | + fmt.Println("flash set OMCI_OLT_MODE 1") |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +// GenerateGponCommands prints the GPON commands. |
| 138 | +func GenerateGponCommands(gponSn, ponVendorId, hwHwver, omciSwVer1, omciSwVer2 string) { |
| 139 | + //fmt.Println("flash set GPON_PLOAM_PASSWD DEFAULT012") |
| 140 | + fmt.Println("flash set GPON_SN " + gponSn) |
| 141 | + fmt.Println("flash set PON_VENDOR_ID " + ponVendorId) |
| 142 | + fmt.Println("## Unplug fiber from Livebox and plug it into UDM and wait a minute ##") |
| 143 | + fmt.Println("Execute this command -> omcicli mib get 131") |
| 144 | + |
| 145 | + fmt.Print("OLT VENDOR ID (HWTC, ALCL, ...) : ") |
| 146 | + scanner := bufio.NewScanner(os.Stdin) |
| 147 | + if scanner.Scan() { |
| 148 | + oltvendorid := scanner.Text() |
| 149 | + generateOMCC(strings.ToUpper(strings.TrimSpace(oltvendorid)), hwHwver, omciSwVer1, omciSwVer2) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// DisplayUDMinfos prints the UDM information. |
| 154 | +func DisplayUDMinfos(vlanid int, macaddress string, dhcpinfos *DHCPInfo) { |
| 155 | + fmt.Println("NAME : LEOX GPON") |
| 156 | + fmt.Println("VLAN ID : " + strconv.Itoa(vlanid)) |
| 157 | + fmt.Println("MAC Address Clone : " + macaddress) |
| 158 | + fmt.Println("DHCP OPTION 60 : " + dhcpinfos.Option60) |
| 159 | + fmt.Println("DHCP OPTION 77 : " + dhcpinfos.Option77) |
| 160 | + fmt.Println("DHCP OPTION 90 : " + dhcpinfos.Option90) |
| 161 | + fmt.Println("DHCP CoS : 6 (Requires Network App 7.4.X or later)") |
| 162 | +} |
| 163 | + |
| 164 | +// DisplayFunboxValues prints the Funbox values. |
| 165 | +func DisplayFunboxValues(client *livebox.Client) error { |
| 166 | + fbvalues, err := GetFunboxValues(client) |
| 167 | + if err != nil { |
| 168 | + return err |
| 169 | + } |
| 170 | + |
| 171 | + fmt.Println("===========UDM PRO SE SETTINGS===========") |
| 172 | + fmt.Println("PPPoE Username : " + fbvalues.Result.Status.Ppp.PppData.Username) |
| 173 | + fmt.Println("PPPoE Password : https://www.orange.pl/moj-orange -> Ustawienia -> Zabezpieczenia -> Hasło do Neostrady -> Zmień hasło") |
| 174 | + fmt.Println("VLAN ID: " + strconv.Itoa(fbvalues.Result.Status.Vlan.GvlanData.Vlanid)) |
| 175 | + fmt.Println("=========================================") |
| 176 | + |
| 177 | + gponSn := fbvalues.Result.Status.Gpon.Veip0.SerialNumber |
| 178 | + ponVendorId := fbvalues.Result.Status.Gpon.Veip0.SerialNumber[0:4] |
| 179 | + hwHwver := fbvalues.Result.Status.Gpon.Veip0.HardwareVersion |
| 180 | + omciSwVer1 := fbvalues.Result.Status.Gpon.Veip0.ONTSoftwareVersion0 |
| 181 | + omciSwVer2 := fbvalues.Result.Status.Gpon.Veip0.ONTSoftwareVersion1 |
| 182 | + |
| 183 | + fmt.Println("") |
| 184 | + fmt.Println("============LEOX GPON COMMAND============") |
| 185 | + GenerateGponCommands(gponSn, ponVendorId, hwHwver, omciSwVer1, omciSwVer2) |
| 186 | + fmt.Println("=========================================") |
| 187 | + return nil |
| 188 | +} |
0 commit comments