Skip to content

Commit

Permalink
initial mvp
Browse files Browse the repository at this point in the history
  • Loading branch information
Jmainguy committed Nov 4, 2019
1 parent be2e167 commit 8109865
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ibsdns
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# ibsdns
InternetBS DNS Updater
## Why?
My buddy Keenan wanted to update home.example.com to point to his IP at home,
however he has a dynamic IP provided to home and so he needed a way to keep it current.
He was insistent on using InternetBS as compared to Route53 as he believed it would be less expensive (free basically)

So I made this for him, though I did it in a way where it can be used by anyone with a InternetBS api key,
and a static host to ssh to and run the update tool from (internetBS limits their IP to a static IP you provide ahead of time)

## HowTo
On server at home (or where the IP will be dynamicly changing) you need to add `grabDynamicIp.sh` to a cronjob, like once every 5 minutes perhaps

```/bin/bash
*/5 * * * * /home/jmainguy/grabDynamicIp.sh [email protected] > /home/jmainguy/dnsLog.txt 2>&1
```

On remotehost add ibsdns binary to `/usr/sbin/ibsdns` and edit `/opt/ibsdns/config.yaml` for your values.

Ensure passwordless ssh is setup from home to remote host, and that the user can read /opt/ibsdns/config.yaml
19 changes: 19 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package main

import (
"github.com/ghodss/yaml"
"io/ioutil"
)

func config() (credents Config) {
config_file, err := ioutil.ReadFile("/opt/ibsdns/config.yaml")
check(err)
yaml.Unmarshal(config_file, &credents)
return
}

type Config struct {
ApiKey string `json:"apiKey"`
Password string `json:"password"`
Domain string `json:"domain"`
}
6 changes: 6 additions & 0 deletions exampleConfig.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Get this from internet.bs
apiKey: RANDOMCHARACTERS
# Get this from intertnet.bs
password: MORERANDOMCHARACHTERS
# Domain you wish to keep updated to the dynamic ip
domain: home.jmainguy.com
16 changes: 16 additions & 0 deletions grabDynamicIp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
CONNECTION=$1
if [[ $CONNECTION == "" ]]; then
echo "Usage ./grabDynamicIp.sh user@remoteHost"
echo "You must pass connection string for remote box with a static IP where ibsdns is installed"
echo "Please setup ssh keys so you can add this to a cronjob"
exit 1
fi
NEWIP=$(curl -s https://ip.jmainguy.com | awk '{print $4}')
OLDIP=$(cat ~/lastIP.txt)
if [[ $NEWIP != $OLDIP ]]; then
echo $NEWIP > ~/lastIP.txt
ssh $CONNECTION "/usr/sbin/ibsdns --value \"$NEWIP\""
else
echo "No update needed"
fi
53 changes: 53 additions & 0 deletions internetbs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

func updateDns(apiKey, password, fullrecordname, recordtype, newvalue string) (transactid, status, message string) {

// Test Api
Url := "https://api.internet.bs/Domain/DnsRecord/Update"

param := url.Values{}
param.Add("apiKey", apiKey)
param.Add("password", password)
param.Add("fullrecordname", fullrecordname)
param.Add("type", recordtype)
param.Add("newvalue", newvalue)
param.Add("ResponseFormat", "JSON")

req, err := http.NewRequest("POST", Url, strings.NewReader(param.Encode()))
if err != nil {
fmt.Println(err)
}
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println(err)
}
defer resp.Body.Close()
jsonblob, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
}
transactid, status, message = decodeResponse(jsonblob)
return
}

func decodeResponse(jsonblob []byte) (transactid, status, message string) {
var response Response
json.Unmarshal(jsonblob, &response)
transactid = response.Transactid
status = response.Status
message = response.Message

return
}
36 changes: 36 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"fmt"
"os"
"flag"
)

func check(e error) {
if e != nil {
fmt.Println(e)
}
}

func main() {

value := flag.String("value", "", "IP to update domain to, example 192.168.1.1")
flag.Parse()

c := config()
recordtype := "A"
newvalue := *value

if newvalue == "" {
fmt.Println("You must provide a value for the IP")
os.Exit(1)
}

tid, status, message := updateDns(c.ApiKey, c.Password, c.Domain, recordtype, newvalue)
if message != "" {
fmt.Printf("TransactID: %s, Status: %s, Message: %s\n", tid, status, message)
} else {
fmt.Printf("TransactID: %s, Status: %s\n", tid, status)
}

}
7 changes: 7 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

type Response struct {
Transactid string `json:"transactid"`
Status string `json:"status"`
Message string `json:"message"`
}

0 comments on commit 8109865

Please sign in to comment.