Skip to content
Merged
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ Option | description
------ | -----------
Repository | Path to your own copy of the repository
Templates | Path containing the templates
GoogleMapsAPIKey | Google Maps API key that you can [obtain here](https://developers.google.com/maps/documentation/javascript/get-api-key).
OutputMode | auto: based on the *Accept* header content<br>redirect: do an HTTP redirect to the destination<br>json: return a JSON formatted document (also known as API mode)
ListenAddress | Local address and port to bind
Gzip | Use gzip compression for the JSON responses
Expand Down
3 changes: 0 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var (
DisallowRedirects: false,
WeightDistributionRange: 1.5,
DisableOnMissingFile: false,
GoogleMapsAPIKey: "",
}
config *Configuration
configMutex sync.RWMutex
Expand Down Expand Up @@ -78,8 +77,6 @@ type Configuration struct {

RedisSentinelMasterName string `yaml:"RedisSentinelMasterName"`
RedisSentinels []sentinels `yaml:"RedisSentinels"`

GoogleMapsAPIKey string `yaml:"GoogleMapsAPIKey"`
}

type fallback struct {
Expand Down
1 change: 0 additions & 1 deletion contrib/docker/mirrorbits.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Repository: /srv/repo
Templates: /go/src/github.com/etix/mirrorbits/templates/
GoogleMapsAPIKey:
OutputMode: auto
ListenAddress: :8080
Gzip: false
Expand Down
2 changes: 0 additions & 2 deletions http/pagerenderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ func (w *MirrorListRenderer) Write(ctx *Context, results *mirrors.Results) (stat
// Create a temporary output buffer to render the page
var buf bytes.Buffer

// Generate the URL for the map
results.MapURL = mirrors.GetMirrorMapURL(results.MirrorList, results.ClientInfo)
ctx.ResponseWriter().Header().Set("Content-Type", "text/html; charset=utf-8")

// Render the page into the buffer
Expand Down
1 change: 0 additions & 1 deletion mirrorbits.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Repository: /srv/repo
Templates: /usr/share/mirrorbits/
GoogleMapsAPIKey: <insert-api-key>
OutputMode: json
ListenAddress: :8080
Gzip: false
Expand Down
37 changes: 0 additions & 37 deletions mirrors/mirrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package mirrors

import (
"bytes"
"fmt"
"math/rand"
"strconv"
Expand Down Expand Up @@ -211,46 +210,10 @@ func SetMirrorState(r *database.Redis, id string, state bool, reason string) err
return err
}

// GetMirrorMapURL returns the URL of a map containing the location of the closest mirrors
// as well as the client's guessed location.
func GetMirrorMapURL(mirrors Mirrors, clientInfo network.GeoIPRecord) string {
var buffer bytes.Buffer
buffer.WriteString("//maps.googleapis.com/maps/api/staticmap?size=600x320&sensor=false&visual_refresh=true")

if key := GetConfig().GoogleMapsAPIKey; key != "" {
buffer.WriteString(fmt.Sprintf("&key=%s", key))
}

if clientInfo.IsValid() {
buffer.WriteString(fmt.Sprintf("&markers=size:mid|color:red|%f,%f", clientInfo.Latitude, clientInfo.Longitude))
}

count := 1
for i, mirror := range mirrors {
if count > 9 {
break
}
if i == 0 && clientInfo.IsValid() {
// Draw a path between the client and the mirror
buffer.WriteString(fmt.Sprintf("&path=color:0x17ea0bdd|weight:5|%f,%f|%f,%f",
clientInfo.Latitude, clientInfo.Longitude,
mirror.Latitude, mirror.Longitude))
}
color := "blue"
if mirror.Weight > 0 {
color = "green"
}
buffer.WriteString(fmt.Sprintf("&markers=color:%s|label:%d|%f,%f", color, count, mirror.Latitude, mirror.Longitude))
count++
}
return buffer.String()
}

// Results is the resulting struct of a request and is
// used by the renderers to generate the final page.
type Results struct {
FileInfo filesystem.FileInfo
MapURL string `json:"-"`
IP string
ClientInfo network.GeoIPRecord
MirrorList Mirrors
Expand Down
63 changes: 0 additions & 63 deletions mirrors/mirrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"time"

"github.com/etix/geoip"
"github.com/etix/mirrorbits/config"
"github.com/etix/mirrorbits/database"
"github.com/etix/mirrorbits/network"
. "github.com/etix/mirrorbits/testing"
Expand Down Expand Up @@ -520,65 +519,3 @@ func TestSetMirrorState(t *testing.T) {
}
}

func TestGetMirrorMapUrl(t *testing.T) {
config.SetConfiguration(&config.Configuration{})

m := Mirrors{
Mirror{
ID: "M0",
Latitude: -80.0,
Longitude: 80.0,
},
Mirror{
ID: "M1",
Latitude: -60.0,
Longitude: 60.0,
},
Mirror{
ID: "M2",
Latitude: -40.0,
Longitude: 40.0,
},
Mirror{
ID: "M3",
Latitude: -20.0,
Longitude: 20.0,
},
}

c := network.GeoIPRecord{
GeoIPRecord: &geoip.GeoIPRecord{
Latitude: -10.0,
Longitude: 10.0,
},
ASNum: 4444,
}

result := GetMirrorMapURL(m, c)

if !strings.HasPrefix(result, "//maps.googleapis.com") {
t.Fatalf("Bad format")
}

if !strings.Contains(result, "color:red") {
t.Fatalf("Missing client marker?")
}

if strings.Count(result, "label:") != len(m) {
t.Fatalf("Missing some mirror markers?")
}

if strings.Contains(result, "key=") {
t.Fatalf("Result should not contain an api key")
}

config.SetConfiguration(&config.Configuration{
GoogleMapsAPIKey: "qwerty",
})

result = GetMirrorMapURL(m, c)

if !strings.Contains(result, "key=qwerty") {
t.Fatalf("Result must contain the api key")
}
}
105 changes: 104 additions & 1 deletion templates/mirrorlist.html
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,45 @@
chart.draw(data, options);
}
</script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.2/leaflet.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.0.0/MarkerCluster.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.2/leaflet.js"></script>
<style>
.numberboxinmapblue {
background:blue; color:white; border:none;
text-align:center;
font-size: 11px;
min-height: 16px; min-width: 16px;
}
.numberboxinmapblue:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0; height: 0;
border: 4px solid transparent;
border-top-color: blue;
border-bottom: 0;
margin-left: -4px; margin-bottom: -4px;
}
.numberboxinmapgreen {
background:green; color:white; border:none;
text-align:center;
font-size: 11px;
min-height: 16px; min-width: 16px;
}
.numberboxinmapgreen:after {
content: '';
position: absolute;
bottom: 0;
left: 50%;
width: 0; height: 0;
border: 4px solid transparent;
border-top-color: green;
border-bottom: 0;
margin-left: -4px; margin-bottom: -4px;
}
</style>
{{end}}

{{define "body"}}
Expand Down Expand Up @@ -53,10 +92,74 @@ <h3>File</h3>
</div>

<div style="display: flex; flex-wrap: wrap; justify-content: space-around;">
<div style=""><img style="border: 1px grey;" width="600" height="320" src="{{.MapURL}}"/></div>
<div id="map" style="width:600; height:320;"></div>
<div id="chart_div" style="width:600; height:320;"></div>
</div>

<script>
var map = L.map('map').setView([20,37], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Data &copy; <a href="https://openstreetmap.org/copyright">OpenStreetMap contributors</a>, map rendering <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
}).addTo(map);

var mapdata = {
"mirrors" : [
{{range $i, $v := .MirrorList}}{{if lt $i 19 }}
{
"lat" : {{$v.Latitude}},
"lon" : {{$v.Longitude}},
"nr" : {{add $i 1}},
"color" : {{if $v.Weight}}"green"{{else}}"blue"{{end}}
},
{{end}}{{end}}
],
"clientpos" : {
{{if .ClientInfo.GeoIPRecord}}
"lat" : {{.ClientInfo.Latitude}},
"lon" : {{.ClientInfo.Longitude}}
{{else}}
"lat" : 0.0,
"lon" : 0.0
{{end}}
}
};
var latlngbounds = new L.latLngBounds(); // For calculating the boundaries we zoom to on map load
if ((mapdata.clientpos.lat != 0.0) || (mapdata.clientpos.lon != 0.0)) {
var nxtmarker = new L.marker([mapdata.clientpos.lat, mapdata.clientpos.lon]);
nxtmarker.addTo(map);
latlngbounds.extend([mapdata.clientpos.lat, mapdata.clientpos.lon]);
if (mapdata.mirrors.length > 0) {
var polyline = L.polyline( [
[ mapdata.clientpos.lat, mapdata.clientpos.lon ],
[ mapdata.mirrors[0].lat, mapdata.mirrors[0].lon ]
],
{color: 'blue', opacity: 0.6});
polyline.addTo(map);
// No need to update the bounds - that happens when the mirrors dot is added below.
}
}
var showaftercutoff = 3; // How many mirrors without weight to use for calculating bounds
for (i in mapdata.mirrors) {
var myIcon = L.divIcon({
className: 'numberboxinmap' + mapdata.mirrors[i].color,
html: "" + mapdata.mirrors[i].nr,
iconSize: null,
iconAnchor: [8, 20]
});
var nxtmarker = new L.marker([mapdata.mirrors[i].lat, mapdata.mirrors[i].lon], {icon: myIcon});
nxtmarker.addTo(map);
if (mapdata.mirrors[i].color != "green") {
showaftercutoff--;
}
if (showaftercutoff > 0) {
latlngbounds.extend([mapdata.mirrors[i].lat, mapdata.mirrors[i].lon]);
}
}
latlngbounds.pad(1);
map.fitBounds(latlngbounds);

</script>

<div>
<br/>
<h3>Mirrors</h3>
Expand Down