Another copy of my dotfiles. Because I don't completely trust GitHub.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
4.2 KiB

4 years ago
  1. #!/bin/bash
  2. readonly SCAN_RESULT=/tmp/connman.scan
  3. readonly STORAGE_PATH=/var/lib/connman
  4. readonly VPN_STORAGE_PATH=/var/lib/connman-vpn
  5. get_services() {
  6. if [[ -f $SCAN_RESULT ]]; then
  7. dmenu_notify 'another connman_dmenu is running'
  8. exit 1
  9. fi
  10. trap "rm -f $SCAN_RESULT" EXIT
  11. connmanctl enable wifi &>/dev/null
  12. connmanctl scan wifi &>/dev/null
  13. connmanctl services | \
  14. awk -F ' +' '{ service_id=$NF; $NF=""; $1=""; name=substr($0, 2, length-2); gsub(/[^a-zA-Z0-9-]/, "_", name) }
  15. name { print name, service_id }' > $SCAN_RESULT
  16. }
  17. # $1 = index
  18. index_to_name() {
  19. [[ -f $SCAN_RESULT ]] || exit 1
  20. awk -v line="$1" 'NR == line { print $1 }' $SCAN_RESULT
  21. }
  22. # $1 = index
  23. index_to_service() {
  24. [[ -f $SCAN_RESULT ]] || exit 1
  25. awk -v line="$1" 'NR == line { print $2 }' $SCAN_RESULT
  26. }
  27. # $1 = service id
  28. get_service_security() {
  29. cut -d _ -f 5 <<<"$1"
  30. }
  31. # $1 = service id
  32. get_service_signal() {
  33. connmanctl services "$1" | awk '$1 == "Strength" { print $3 }'
  34. }
  35. # $1 = service id
  36. get_service_state() {
  37. connmanctl services "$1" | awk '$1 == "State" { print $3 }'
  38. }
  39. create_dmenu() {
  40. [[ -f $SCAN_RESULT ]] || exit 1
  41. echo 'setup vpn pptp'
  42. local order=1
  43. local name
  44. local service_id
  45. local security
  46. local signal
  47. local disconnect
  48. while read -r name service_id; do
  49. security=''
  50. signal=''
  51. disconnect=''
  52. [[ ! "$(get_service_state "$service_id")" =~ ^(idle|failure)$ ]] && disconnect='(disconnect)'
  53. case "$service_id" in
  54. wifi_*)
  55. security="$(get_service_security "$service_id")"
  56. signal="$(get_service_signal "$service_id")"
  57. ;;
  58. vpn_*)
  59. security=vpn
  60. ;;
  61. esac
  62. printf '%2s %-40s%9s %-3s %s\n' "$order" "$name" "$security" "$signal" "$disconnect"
  63. (( order++ ))
  64. done < $SCAN_RESULT
  65. }
  66. # $1 = msg
  67. dmenu_notify() {
  68. : | dmenu -p "$1 (press enter)"
  69. }
  70. # $1 = question
  71. # $2 = var name
  72. dmenu_ask() {
  73. IFS= read -r "$2" < <(: | dmenu -p "$1")
  74. if [[ ! "${!2}" ]]; then
  75. dmenu_notify "invalid $2"
  76. exit 1
  77. fi
  78. }
  79. get_services
  80. index="$(create_dmenu | dmenu -l 10 -i -p 'select wifi service' | sed 's/^ *//g' | cut -d ' ' -f 1)"
  81. if [[ "$index" == setup ]] ; then
  82. # create vpn mode
  83. dmenu_ask 'name this PPTP VPN' name
  84. name="${name// /_}"
  85. dmenu_ask 'please provide VPN domain' domain
  86. dmenu_ask 'please provide identity' identity
  87. dmenu_ask 'please provide password' password
  88. cat > "$VPN_STORAGE_PATH/$name.config" <<-EOF
  89. [provider_$name]
  90. Type = PPTP
  91. Name = $name
  92. Host = $(dig +short A "$domain" | sort -n | head -n1)
  93. Domain = $domain
  94. PPTP.User = $identity
  95. PPTP.Password = $password
  96. EOF
  97. dmenu_notify "VPN $name is created"
  98. exit 0
  99. fi
  100. service_id="$(index_to_service "$index")"
  101. [[ "$service_id" ]] || exit 1
  102. name="$(index_to_name "$index")"
  103. echo "$name { $service_id }"
  104. if [[ ! "$(get_service_state "$service_id")" =~ ^(idle|failure)$ ]]; then
  105. connmanctl disconnect "$service_id"
  106. dmenu_notify "$name disconnected"
  107. exit 0
  108. fi
  109. security="$(get_service_security "$service_id")"
  110. # create service file for encryption
  111. if [[ "$security" =~ ^(ieee8021x|psk|wep)$ ]]; then
  112. config_file="$STORAGE_PATH/$name-$security.config"
  113. if [[ -f "$config_file" && no != "$(echo -e 'yes\nno' | dmenu -p 'use previous profile?')" ]]; then
  114. echo "use old profile: $config_file"
  115. else
  116. dmenu_ask 'please provide password' password
  117. case "$security" in
  118. ieee8021x)
  119. dmenu_ask 'please provide identity' identity
  120. case "$(echo -e 'PEAP/MSCHAPV2\nTTLS/PAP' | dmenu -p 'please specify EAP type')" in
  121. PEAP/MSCHAPV2)
  122. eap=peap
  123. phase2=MSCHAPV2
  124. ;;
  125. TTLS/PAP)
  126. eap=ttls
  127. phase2=PAP
  128. ;;
  129. *)
  130. dmenu_notify 'invalid EAP'
  131. exit 1
  132. ;;
  133. esac
  134. cat > "$config_file" <<-EOF
  135. [service_$service_id]
  136. Type = wifi
  137. Name = $name
  138. EAP = $eap
  139. Phase2 = $phase2
  140. Identity = $identity
  141. Passphrase = $password
  142. EOF
  143. ;;
  144. psk|wep)
  145. cat > "$config_file" <<-EOF
  146. [service_$service_id]
  147. Type = wifi
  148. Name = $name
  149. Passphrase = $password
  150. EOF
  151. ;;
  152. esac
  153. chmod 600 "$config_file"
  154. fi
  155. fi
  156. connman_msg="$(timeout 10 connmanctl connect "$service_id" 2>&1 | head -n 1)"
  157. if [[ "$connman_msg" == Connected* ]]; then
  158. dmenu_notify "connected to $name"
  159. else
  160. error_msg='automatic timeout for connman_dmenu'
  161. [[ "$connman_msg" ]] && error_msg="$(cut -d ' ' -f 3- <<<"$connman_msg")"
  162. dmenu_notify "cannot connect to $name ($error_msg)"
  163. fi