Skip to content

Implemented ping capability to get the online status of the computers #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/publish_image.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Create and publish a Docker image

on:
push:
tags:
- '*'

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ RUN apk update && apk upgrade && \
apk add --no-cache git && \
go get -d github.com/gorilla/handlers@v1.5.1 && \
go get -d github.com/gorilla/mux@v1.8.0 && \
go get -d github.com/gocarina/gocsv@v0.0.0-20220727205534-7fbf8e1b37fb
go get -d github.com/gocarina/gocsv@v0.0.0-20220727205534-7fbf8e1b37fb && \
go get -d github.com/tatsushid/go-fastping@v0.0.0-20160109021039-d7bb493dee3e

# Build Source Files
RUN go build
Expand Down
8 changes: 4 additions & 4 deletions computer.csv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name,mac,ip
Computer1,64-07-2D-BB-BB-BF,192.168.10.254:9
Computer2,2D-F2-3D-06-17-00,192.168.10.254:9
Computer3,FF-B3-95-62-1C-DD,192.168.10.254:9
name,mac,ip,broadcast_ip
Computer1,64-07-2D-BB-BB-BF,192.168.10.100,192.168.10.254:9
Computer2,2D-F2-3D-06-17-00,192.168.10.101,192.168.10.254:9
Computer3,FF-B3-95-62-1C-DD,192.168.10.102,192.168.10.254:9
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ func main() {
// Define Wakeup Api functions with a Computer Name
router.HandleFunc("/api/wakeup/computer/{computerName}", restWakeUpWithComputerName).Methods("GET")
router.HandleFunc("/api/wakeup/computer/{computerName}/", restWakeUpWithComputerName).Methods("GET")
router.HandleFunc("/api/ping/computer/{computerName}", pingWithComputerName).Methods("GET")
router.HandleFunc("/api/ping/computer/{computerName}/", pingWithComputerName).Methods("GET")

// Setup Webserver
httpListen := fmt.Sprint(":", httpPort)
Expand Down
90 changes: 84 additions & 6 deletions pages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,41 @@
<div class="row">
<div class="col-12" role="main">
<h1><span class="fa fa-qq"></span> GO-REST-WOL <small>Your little Monster to Wakeup the lazy one</small></h1>
<div id="snackbar" class="alert hideMe">
<div id="snackbar" class="alert hideMe" style="display:none">
Message comes here
</div>
<table class="table">
<thead>
<tr>
<th></td>
<th></th>
<th>Computer</th>
<th>Status</th>
<th>MAC Adress</th>
<th>Broadcast IP Address</th>
<th>IP Address</th>
<th>API URL</th>
<th>API Status URL</th>
</tr>
</thead>
<tbody>
{{range .}}
<tr>
<tr id="computer_{{.Name}}">
<td class="align-middle"><b><button id="bn_{{.Name}}" name="wolbutton" value="{{.Name}}" class="btn btn-success btn-sm" onclick="$.wakeUpComputerByName('{{.Name}}')">WakeUp</button></b></td>
<td class="align-middle">{{.Name}}</td>
<td class="align-middle computername">{{.Name}}</td>
<td class="align-middle">
<span style="display:none" id="online_{{.Name}}" class="badge badge-success">UP</span>
<span id="offline_{{.Name}}" class="badge badge-danger">DOWN</span>
</td>
<td class="align-middle">{{.Mac}}</td>
<td class="align-middle">{{.BroadcastIPAddress}}</td>
<td class="align-middle">/api/wakeup/computer/{{.Name}}</td>
<td class="align-middle">/api/ping/computer/{{.Name}}</td>
</tr>
{{end}}
</tbody>
</table>
<hr>
<p>
<h3>REST API Usage</h3>
<h3>REST API Usage: WOL</h3>
<b>/api/wakeup/computer/<span class="text-info">&lt;ComputerName&gt;</span></b>

<b>Returns a JSON Object</b>
Expand All @@ -69,6 +76,33 @@ <h3>REST API Usage</h3>
</p>
<hr>
<p>

<p>
<h3>REST API Usage: PING</h3>
<b>/api/ping/computer/<span class="text-info">&lt;ComputerName&gt;</span></b>

<b>Returns a JSON Object</b>
<p>
<pre>
{
"isonline":true,
"message":"Computer Computer1 with IP Address 192.168.10.101 is online!",
"error":null
}
</pre>
</p>
<dl class="dl-horizontal">
<dt>isonline</dt>
<dd>True or False if the Ping packet was sent and a response was received.</dd>
<dt>message</dt>
<dd>Message as string what happen</dd>
<dt>error</dt>
<dd>Encoded Jsonobject from GOLANG Error Object</dd>
</dl>

</p>
<hr>
<p>
<span class="fa fa-github"></span> Project Page: <a href="https://github.com/dabondi/go-rest-wol">https://github.com/dabondi/go-rest-wol</a>
</p>
<p>
Expand All @@ -84,6 +118,50 @@ <h3>REST API Usage</h3>

<script>
$(document).ready(function() {
pingComputers()
setInterval(pingComputers, 5000);

function pingComputers() {
const computerArray = $(".computername").toArray().map(a=> a.innerText);
computerArray.forEach(computer => {
pingComputer(computer);
});
}

function pingComputer(computer){
$.ajax({
type: "GET",
url: "/api/ping/computer/" + computer,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
setComputerStatus(computer,data.isonline)
},
error: function(data,err)
{
$.showSnackBar(data);
console.error(data);
}
});
}

function setComputerStatus(computer,isOnLine)
{
const offLineClass="";
const onLineClass="table-success";
const classToAdd = isOnLine ? onLineClass: offLineClass;
const classToRemove = !isOnLine? onLineClass: offLineClass;
if (isOnLine) {
$(`#offline_${computer}`).hide();
$(`#online_${computer}`).show();
} else {
$(`#offline_${computer}`).show();
$(`#online_${computer}`).hide();
}
$(`#computer_${computer}`)
.removeClass(classToRemove)
.addClass(classToAdd);
}

jQuery.showSnackBar = function(data){

Expand Down
29 changes: 29 additions & 0 deletions ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"net"
"time"

"github.com/tatsushid/go-fastping"
)

// Ping make a ping to an address and retun true of false
func Ping(ipAddress string) (bool, error) {
ret := false

p := fastping.NewPinger()
ra, err := net.ResolveIPAddr("ip4:icmp", ipAddress)
if err != nil {
return false, err
}
p.AddIPAddr(ra)
p.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
ret = true
}

err = p.Run()
if err != nil {
return false, err
}
return ret, nil
}
31 changes: 31 additions & 0 deletions rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,34 @@ func restWakeUpWithComputerName(w http.ResponseWriter, r *http.Request) {
}
json.NewEncoder(w).Encode(result)
}

func pingWithComputerName(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

vars := mux.Vars(r)
computerName := vars["computerName"]
var result PingResponseObject
result.IsOnLine=false

var computer Computer
for _,c := range ComputerList{
if c.Name == computerName{
computer = c

result.IsOnLine, result.ErrorObject = Ping(computer.IPAddress)

if result.ErrorObject != nil {
// We got an internal Error on Ping
w.WriteHeader(http.StatusInternalServerError)
result.Message = "Internal error on Sending Ping"
} else {
if result.IsOnLine {
result.Message = fmt.Sprintf("Computer %s with IP Address %s is online!", computerName, computer.IPAddress)
} else {
result.Message = fmt.Sprintf("Computer %s with IP Address %s is offline!", computerName, computer.IPAddress)
}
}
}
}
json.NewEncoder(w).Encode(result)
}
9 changes: 8 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,12 @@ type WakeUpResponseObject struct {
type Computer struct {
Name string `csv:"name"`
Mac string `csv:"mac"`
BroadcastIPAddress string `csv:"ip"`
IPAddress string `csv:"ip"`
BroadcastIPAddress string `csv:"broadcast_ip"`
}

type PingResponseObject struct {
IsOnLine bool `json:"isonline"`
Message string `json:"message"`
ErrorObject error `json:"error"`
}