-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hotspot): add access point on timer using dhcp, dnsmasg, hostapd
- Loading branch information
Showing
4 changed files
with
266 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Note this file has no ssid as it is set by the script | ||
country_code=GB | ||
interface=wlan0 | ||
hw_mode=g | ||
channel=7 | ||
macaddr_acl=0 | ||
auth_algs=1 | ||
ignore_broadcast_ssid=0 | ||
wpa=2 | ||
wpa_key_mgmt=WPA-PSK | ||
wpa_pairwise=TKIP | ||
rsn_pairwise=CCMP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
) | ||
|
||
// refactor createAPConfig to remove duplication | ||
func createAPConfig(name string) error { | ||
file_name := "/etc/hostapd/hostapd.conf" | ||
config_lines := []string{ | ||
"country_code=NZ", | ||
"interface=wlan0", | ||
"ssid=" + name, | ||
"hw_mode=g", | ||
"channel=7", | ||
"macaddr_acl=0", | ||
"ignore_broadcast_ssid=0", | ||
"wpa=2", | ||
"wpa_passphrase=feathers", | ||
"wpa_key_mgmt=WPA-PSK", | ||
"wpa_pairwise=TKIP", | ||
"rsn_pairwise=CCMP", | ||
} | ||
return creatConfigFile(file_name, config_lines) | ||
} | ||
|
||
func startAccessPoint(name string) error { | ||
return exec.Command("systemctl", "restart", "hostapd").Start() | ||
} | ||
|
||
func stopAccessPoint() error { | ||
return exec.Command("systemctl", "stop", "hostapd").Start() | ||
} | ||
|
||
const router_ip = "192.168.4.1" | ||
|
||
var dhcp_config_default = []string{ | ||
"hostname", | ||
"clientid", | ||
"persistent", | ||
"option rapid_commit", | ||
"option domain_name_servers, domain_name, domain_search, host_name", | ||
"option classless_static_routes", | ||
"option ntp_servers", | ||
"option interface_mtu", | ||
"require dhcp_server_identifier", | ||
"slaac private", | ||
} | ||
|
||
var dhcp_config_lines = []string{ | ||
"interface wlan0", | ||
"static ip_address=" + router_ip + "/24", | ||
"nohook wpa_supplicant", | ||
} | ||
|
||
func createDHCPConfig() error { | ||
file_path := "/etc/dhcpcd.conf" | ||
|
||
// append to dhcpcd.conf if lines don't already exist | ||
config_lines := append(dhcp_config_default, dhcp_config_lines...) | ||
if err := writeLines(file_path, config_lines); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func removeLines(file_path string, removed_lines []string) error { | ||
file, err := os.Open(file_path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
var lines []string | ||
scanner := bufio.NewScanner(file) | ||
for scanner.Scan() { | ||
line := scanner.Text() | ||
if !contains(removed_lines, line) { | ||
lines = append(lines, line) | ||
} | ||
} | ||
|
||
return writeLines(file_path, lines) | ||
} | ||
|
||
func writeLines(file_path string, lines []string) error { | ||
file, err := os.Create(file_path) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
w := bufio.NewWriter(file) | ||
for _, line := range lines { | ||
_, _ = fmt.Fprintln(w, line) | ||
} | ||
return w.Flush() | ||
} | ||
|
||
func contains(lines []string, line string) bool { | ||
for _, l := range lines { | ||
if strings.TrimSpace(l) == strings.TrimSpace(line) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func startDHCP() error { | ||
// modify /etc/dhcpcd.conf | ||
if err := createDHCPConfig(); err != nil { | ||
return err | ||
} | ||
|
||
return exec.Command("systemctl", "restart", "dhcpcd").Run() | ||
} | ||
|
||
func restartDHCP() error { | ||
if err := writeLines("/etc/dhcpcd.conf", dhcp_config_default); err != nil { | ||
return err | ||
} | ||
return exec.Command("systemctl", "restart", "dhcpcd").Run() | ||
} | ||
|
||
func checkIsConnectedToNetwork() error { | ||
// check if network is up | ||
if err := exec.Command("iwgetid", "wlan0", "-r").Run(); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func createDNSConfig(router_ip string, ip_range string) error { | ||
// DNSMASQ config | ||
file_name := "/etc/dnsmasq.conf" | ||
config_lines := []string{ | ||
"interface=wlan0", | ||
"dhcp-range=" + ip_range + ",12h", | ||
"domain=wlan", | ||
"address=/#/" + router_ip, | ||
} | ||
return creatConfigFile(file_name, config_lines) | ||
|
||
} | ||
|
||
func startDNS() error { | ||
return exec.Command("systemctl", "restart", "dnsmasq").Start() | ||
} | ||
|
||
func stopDNS() error { | ||
return exec.Command("systemctl", "stop", "dnsmasq").Start() | ||
} | ||
|
||
func creatConfigFile(name string, config []string) error { | ||
file, err := os.Create(name) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
w := bufio.NewWriter(file) | ||
for _, line := range config { | ||
_, err = fmt.Fprintln(w, line) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
err = w.Flush() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters