mirror of
https://github.com/schollz/hostyoself.git
synced 2026-07-18 00:45:58 +00:00
add utils
This commit is contained in:
parent
79fbf0e1c4
commit
8b13dde599
4 changed files with 379 additions and 162 deletions
2
go.mod
2
go.mod
|
|
@ -1,4 +1,4 @@
|
|||
module github.com/schollz/userve
|
||||
module github.com/schollz/omniserve
|
||||
|
||||
go 1.12
|
||||
|
||||
|
|
|
|||
105
main.go
105
main.go
|
|
@ -6,19 +6,21 @@ import (
|
|||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/h2non/filetype"
|
||||
log "github.com/schollz/logger"
|
||||
"github.com/vincent-petithory/dataurl"
|
||||
"github.com/h2non/filetype"
|
||||
)
|
||||
|
||||
type server struct {
|
||||
port string
|
||||
publicURL string
|
||||
port string
|
||||
|
||||
// connections stored as map of domain -> connections
|
||||
conn map[string][]*Connection
|
||||
|
|
@ -61,8 +63,9 @@ func (ws *WebsocketConn) Receive() (p Payload, err error) {
|
|||
|
||||
func main() {
|
||||
var debug bool
|
||||
var flagPort string
|
||||
var flagPort, flagPublicURL string
|
||||
flag.StringVar(&flagPort, "port", "8001", "port")
|
||||
flag.StringVar(&flagPublicURL, "url", "", "public url to use")
|
||||
flag.BoolVar(&debug, "debug", false, "debug mode")
|
||||
flag.Parse()
|
||||
|
||||
|
|
@ -72,11 +75,19 @@ func main() {
|
|||
log.SetLevel("info")
|
||||
}
|
||||
|
||||
s := new(server)
|
||||
s.Lock()
|
||||
s.port = flagPort
|
||||
s.conn = make(map[string][]*Connection)
|
||||
s.Unlock()
|
||||
if flagPublicURL == "" {
|
||||
flagPublicURL = "localhost:" + flagPort
|
||||
}
|
||||
if !strings.HasPrefix(flagPublicURL, "http") {
|
||||
flagPublicURL = "http://" + flagPublicURL
|
||||
}
|
||||
|
||||
s := &server{
|
||||
port: flagPort,
|
||||
conn: make(map[string][]*Connection),
|
||||
publicURL: flagPublicURL,
|
||||
}
|
||||
|
||||
s.serve()
|
||||
}
|
||||
|
||||
|
|
@ -97,7 +108,7 @@ func (s *server) handler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (s *server) handle(w http.ResponseWriter, r *http.Request) (err error) {
|
||||
|
||||
log.Debugf("URL: %s, Referer: %s", r.URL.Path, r.Referer())
|
||||
// very special paths
|
||||
if r.URL.Path == "/robots.txt" {
|
||||
// special path
|
||||
|
|
@ -117,14 +128,47 @@ Disallow: /`))
|
|||
return err
|
||||
}
|
||||
type view struct {
|
||||
Title string
|
||||
HTML string
|
||||
PublicURL string
|
||||
Title string
|
||||
HTML string
|
||||
}
|
||||
return t.Execute(w, view{})
|
||||
return t.Execute(w, view{PublicURL: s.publicURL})
|
||||
} else {
|
||||
log.Debugf("attempting to find %s", r.URL.Path)
|
||||
|
||||
pathToFile := r.URL.Path[1:]
|
||||
domain := strings.Split(r.URL.Path[1:], "/")[0]
|
||||
// check to make sure it has domain prepended
|
||||
piecesOfReferer := strings.Split(r.Referer(), "/")
|
||||
if len(piecesOfReferer) > 4 {
|
||||
domain = piecesOfReferer[3]
|
||||
}
|
||||
|
||||
// prefix the domain if it doesn't exist
|
||||
if !strings.HasPrefix(pathToFile, domain) {
|
||||
pathToFile = domain + "/" + pathToFile
|
||||
http.Redirect(w, r, "/"+pathToFile, 302)
|
||||
return
|
||||
}
|
||||
|
||||
// add index.html if it doesn't exist
|
||||
if filepath.Ext(pathToFile) == "" {
|
||||
if string(pathToFile[len(pathToFile)-1]) != "/" {
|
||||
pathToFile += "/"
|
||||
}
|
||||
pathToFile += "index.html"
|
||||
http.Redirect(w, r, "/"+pathToFile, 302)
|
||||
return
|
||||
}
|
||||
|
||||
var ipAddress string
|
||||
ipAddress, err = GetClientIPHelper(r)
|
||||
if err != nil {
|
||||
log.Debugf("could not determine ip: %s", err.Error())
|
||||
}
|
||||
|
||||
var data string
|
||||
data, err = s.get(r.URL.Path[1:])
|
||||
data, err = s.get(pathToFile, ipAddress)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -134,10 +178,22 @@ Disallow: /`))
|
|||
return
|
||||
}
|
||||
contentType := dataURL.MediaType.ContentType()
|
||||
if contentType == "application/octet-stream" {
|
||||
mimeType := filetype.GetType(r.URL.Path[1:])
|
||||
if contentType == "application/octet-stream" || contentType == "" {
|
||||
pathToFileExt := filepath.Ext(pathToFile)
|
||||
mimeType := filetype.GetType(pathToFileExt)
|
||||
contentType = mimeType.MIME.Value
|
||||
if contentType == "" {
|
||||
switch pathToFileExt {
|
||||
case ".css":
|
||||
contentType = "text/css"
|
||||
case ".js":
|
||||
contentType = "text/javascript"
|
||||
case ".html":
|
||||
contentType = "text/html"
|
||||
}
|
||||
}
|
||||
}
|
||||
log.Debugf("%s content-type: '%s'", pathToFile, contentType)
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Write(dataURL.Data)
|
||||
return
|
||||
|
|
@ -155,9 +211,10 @@ var wsupgrader = websocket.Upgrader{
|
|||
|
||||
type Payload struct {
|
||||
// message meta
|
||||
Type string `json:"type"`
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
Success bool `json:"success"`
|
||||
Message string `json:"message"`
|
||||
IPAddress string `json:"ip"`
|
||||
}
|
||||
|
||||
func (p Payload) String() string {
|
||||
|
|
@ -219,7 +276,7 @@ func (s *server) handleWebsocket(w http.ResponseWriter, r *http.Request) (err er
|
|||
return
|
||||
}
|
||||
|
||||
func (s *server) get(filePath string) (payload string, err error) {
|
||||
func (s *server) get(filePath, ipAddress string) (payload string, err error) {
|
||||
log.Debugf("requesting %s", filePath)
|
||||
domain := strings.Split(filePath, "/")[0]
|
||||
|
||||
|
|
@ -239,8 +296,9 @@ func (s *server) get(filePath string) (payload string, err error) {
|
|||
var p Payload
|
||||
p, err = func() (p Payload, err error) {
|
||||
err = conn.ws.Send(Payload{
|
||||
Type: "get",
|
||||
Message: filePath,
|
||||
Type: "get",
|
||||
Message: filePath,
|
||||
IPAddress: ipAddress,
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -253,7 +311,6 @@ func (s *server) get(filePath string) (payload string, err error) {
|
|||
s.DumpConnection(domain, conn.ID)
|
||||
continue
|
||||
}
|
||||
log.Debugf("recv: %+v", p)
|
||||
if p.Type == "get" {
|
||||
payload = p.Message
|
||||
if !p.Success {
|
||||
|
|
@ -261,6 +318,10 @@ func (s *server) get(filePath string) (payload string, err error) {
|
|||
}
|
||||
return
|
||||
}
|
||||
if len(p.Message) > 10 {
|
||||
p.Message = p.Message[:10] + "..."
|
||||
}
|
||||
log.Debugf("recv: %+v", p)
|
||||
err = fmt.Errorf("invalid response")
|
||||
break
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,168 +9,233 @@
|
|||
<meta name="theme-color" content="#ffffff">
|
||||
<link rel="stylesheet" href="https://share.schollz.com/static/dropzone.css">
|
||||
<link rel="stylesheet" href="https://share.schollz.com/static/style.css">
|
||||
<title>Share a file</title>
|
||||
<title>omniserver</title>
|
||||
<style>
|
||||
.main {
|
||||
padding-top: 20px;
|
||||
}
|
||||
|
||||
.list {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.list>div {
|
||||
padding: 0.4em;
|
||||
}
|
||||
|
||||
body {
|
||||
text-decoration-skip: ink;
|
||||
}
|
||||
|
||||
.hide {
|
||||
display: none;
|
||||
}
|
||||
|
||||
textarea {
|
||||
width: 100%;
|
||||
border: none;
|
||||
resize: none;
|
||||
height: 20em;
|
||||
border: 1px solid #ccc;
|
||||
background-color: #f5f5f5;
|
||||
padding: 1em;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main>
|
||||
<h1 align="center"><a href="/">omniserver</a> </h1>
|
||||
<p id="errormessage" class="error"></p>
|
||||
<p>Serve your files from your browser at this domain: <strong>{{.PublicURL}}</strong>.</p>
|
||||
<details>
|
||||
<summary>Click here for FAQ.</summary>
|
||||
</details>
|
||||
<div id="filesBox" class="dropzone">
|
||||
<div class="dz-message" data-dz-message><span>Drop or click here to share a folder.<br>
|
||||
<p><small>Max file size: 100 MB</small></p>
|
||||
<div class="dz-message" data-dz-message><span>Drop or folder or click here to share a file.<br>
|
||||
<p><small></small></p>
|
||||
</span></div>
|
||||
</div>
|
||||
<div id="console" class="dropzone hide">
|
||||
<div id="consoleHeader"></div>
|
||||
<details>
|
||||
<summary>Files served</summary>
|
||||
<div id="fileList"></div>
|
||||
</details>
|
||||
<h3>Console:</h3>
|
||||
<textarea id="consoleText" readonly></textarea>
|
||||
</div>
|
||||
<footer>
|
||||
<!-- <p align="center" style="margin-bottom:0">
|
||||
<img src="/static/logo47.png" style="max-width: 100px">
|
||||
</p>
|
||||
-->
|
||||
<p align="center">Made by <a href="https://github.com/schollz">schollz</a>, source available on <a href="https://github.com/schollz/omniserve">Github</a>. <a href="/static/terms.html">Terms of Use</a>.</p>
|
||||
</footer>
|
||||
</main>
|
||||
|
||||
|
||||
<script src="https://share.schollz.com/static/dropzone.js"></script>
|
||||
<script>
|
||||
var files = [];
|
||||
var isConnected = false;
|
||||
var files = [];
|
||||
var isConnected = false;
|
||||
|
||||
function humanFileSize(bytes, si) {
|
||||
var thresh = si ? 1000 : 1024;
|
||||
if (Math.abs(bytes) < thresh) {
|
||||
return bytes + ' B';
|
||||
}
|
||||
var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB',
|
||||
'EiB', 'ZiB', 'YiB'
|
||||
];
|
||||
var u = -1;
|
||||
do {
|
||||
bytes /= thresh;
|
||||
++u;
|
||||
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
|
||||
return bytes.toFixed(1) + ' ' + units[u];
|
||||
function consoleLog(s) {
|
||||
console.log(s);
|
||||
if (typeof s === 'object') {
|
||||
s = JSON.stringify(s);
|
||||
}
|
||||
|
||||
var Name = "";
|
||||
var filesize = 0;
|
||||
|
||||
(function (Dropzone) {
|
||||
Dropzone.autoDiscover = false;
|
||||
|
||||
let drop = new Dropzone('div#filesBox', {
|
||||
maxFiles: 1000,
|
||||
url: '/',
|
||||
method: 'post',
|
||||
createImageThumbnails: false,
|
||||
previewTemplate: "<div id='preview' class='.dropzone-previews'></div>",
|
||||
autoProcessQueue: false,
|
||||
});
|
||||
|
||||
drop.on('addedfile', function (file) {
|
||||
files.push(file);
|
||||
console.log(file);
|
||||
domain = files[0].webkitRelativePath.split("/")[0];
|
||||
if (domain == "") {
|
||||
domain = files[0].name;
|
||||
}
|
||||
if (!(isConnected)) {
|
||||
isConnected = true;
|
||||
socketSend({
|
||||
"type": "domain",
|
||||
"message": domain,
|
||||
})
|
||||
}
|
||||
html = `<p>Your domain: /${domain}</p>
|
||||
|
||||
<ul>`
|
||||
for (i = 0; i < files.length; i++) {
|
||||
console.log(files[i]);
|
||||
var urlToFile = files[i].name;
|
||||
|
||||
if ('fullPath' in files[i]) {
|
||||
urlToFile = files[i].fullPath;
|
||||
}
|
||||
html = html +
|
||||
`<li><a href="/${urlToFile}" target="_blank">/${urlToFile}</a></li>`
|
||||
}
|
||||
html = html + `</ul>`;
|
||||
document.getElementById("preview").innerHTML = html;
|
||||
})
|
||||
|
||||
})(Dropzone);
|
||||
|
||||
var socket; // websocket
|
||||
|
||||
|
||||
/* websockets */
|
||||
function socketSend(data) {
|
||||
if (socket == null) {
|
||||
return
|
||||
}
|
||||
if (socket.readyState != 1) {
|
||||
return
|
||||
}
|
||||
jsonData = JSON.stringify(data);
|
||||
socket.send(jsonData);
|
||||
if (jsonData.length > 100) {
|
||||
jsonData = jsonData.substring(0, 99) + "...";
|
||||
}
|
||||
console.log("[debug] ws-> " + jsonData)
|
||||
if (!(s.startsWith("[debug]"))) {
|
||||
document.getElementById("consoleText").value = document.getElementById("consoleText").value + s + "\n";
|
||||
document.getElementById("consoleText").scrollTop = document.getElementById("consoleText").scrollHeight;
|
||||
}
|
||||
}
|
||||
|
||||
const socketMessageListener = (event) => {
|
||||
var data = JSON.parse(event.data);
|
||||
if (!('type' in data && 'message' in data)) {
|
||||
console.log(`[warn] got bad data ${event.data}`);
|
||||
return
|
||||
function humanFileSize(bytes, si) {
|
||||
var thresh = si ? 1000 : 1024;
|
||||
if (Math.abs(bytes) < thresh) {
|
||||
return bytes + ' B';
|
||||
}
|
||||
var units = si ? ['kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'] : ['KiB', 'MiB', 'GiB', 'TiB', 'PiB',
|
||||
'EiB', 'ZiB', 'YiB'
|
||||
];
|
||||
var u = -1;
|
||||
do {
|
||||
bytes /= thresh;
|
||||
++u;
|
||||
} while (Math.abs(bytes) >= thresh && u < units.length - 1);
|
||||
return bytes.toFixed(1) + ' ' + units[u];
|
||||
}
|
||||
|
||||
var Name = "";
|
||||
var filesize = 0;
|
||||
|
||||
(function(Dropzone) {
|
||||
Dropzone.autoDiscover = false;
|
||||
|
||||
let drop = new Dropzone('div#filesBox', {
|
||||
maxFiles: 1000,
|
||||
url: '/',
|
||||
method: 'post',
|
||||
createImageThumbnails: false,
|
||||
previewTemplate: "<div id='preview' class='.dropzone-previews'></div>",
|
||||
autoProcessQueue: false,
|
||||
});
|
||||
|
||||
drop.on('addedfile', function(file) {
|
||||
files.push(file);
|
||||
domain = files[0].webkitRelativePath.split("/")[0];
|
||||
if (domain == "") {
|
||||
domain = files[0].name;
|
||||
}
|
||||
console.log(`[debug] ${data.type} ${data.message}`)
|
||||
if (data.type == "get") {
|
||||
var foundFile = false
|
||||
for (i = 0; i < files.length; i++) {
|
||||
if (files[i].webkitRelativePath == data.message || files[i].name == data.message) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function (theFile) {
|
||||
socketSend({
|
||||
type: "get",
|
||||
message: reader.result,
|
||||
success: true,
|
||||
})
|
||||
console.log(`[info] ${data.message} 200`);
|
||||
};
|
||||
reader.readAsDataURL(files[i]);
|
||||
foundFile = true
|
||||
break
|
||||
}
|
||||
if (!(isConnected)) {
|
||||
isConnected = true;
|
||||
socketSend({
|
||||
"type": "domain",
|
||||
"message": domain,
|
||||
})
|
||||
}
|
||||
document.getElementById("consoleHeader").innerHTML = `<p>Your files are served at: <strong><a href="/${domain}" target="_blank">{{.PublicURL}}/${domain}</a></strong></p>`;
|
||||
html = `<ul>`
|
||||
for (i = 0; i < files.length; i++) {
|
||||
var urlToFile = files[i].name;
|
||||
|
||||
if ('fullPath' in files[i]) {
|
||||
urlToFile = files[i].fullPath;
|
||||
}
|
||||
if (foundFile == false) {
|
||||
socketSend({
|
||||
type: "get",
|
||||
message: "not found",
|
||||
success: false,
|
||||
})
|
||||
console.log(`[info] ${data.message} 404`);
|
||||
html = html +
|
||||
`<li><a href="/${urlToFile}" target="_blank">/${urlToFile}</a></li>`
|
||||
}
|
||||
html = html + `</ul>`;
|
||||
document.getElementById("fileList").innerHTML = html;
|
||||
document.getElementById("filesBox").classList.add("hide");
|
||||
document.getElementById("console").classList.remove("hide");
|
||||
|
||||
})
|
||||
|
||||
})(Dropzone);
|
||||
|
||||
var socket; // websocket
|
||||
|
||||
|
||||
/* websockets */
|
||||
function socketSend(data) {
|
||||
if (socket == null) {
|
||||
return
|
||||
}
|
||||
if (socket.readyState != 1) {
|
||||
return
|
||||
}
|
||||
jsonData = JSON.stringify(data);
|
||||
socket.send(jsonData);
|
||||
if (jsonData.length > 100) {
|
||||
jsonData = jsonData.substring(0, 99) + "...";
|
||||
}
|
||||
consoleLog("[debug] ws-> " + jsonData)
|
||||
}
|
||||
|
||||
const socketMessageListener = (event) => {
|
||||
var data = JSON.parse(event.data);
|
||||
if (!('type' in data && 'message' in data)) {
|
||||
consoleLog(`[warn] got bad data ${event.data}`);
|
||||
return
|
||||
}
|
||||
console.log(data)
|
||||
consoleLog(`[debug] ${data.message}`)
|
||||
if (data.type == "get") {
|
||||
var foundFile = false
|
||||
for (i = 0; i < files.length; i++) {
|
||||
if (files[i].webkitRelativePath == data.message || files[i].name == data.message) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function(theFile) {
|
||||
socketSend({
|
||||
type: "get",
|
||||
message: reader.result,
|
||||
success: true,
|
||||
})
|
||||
consoleLog(`${data.ip} [${(new Date()).toUTCString()}] /${data.message} 200 ${files[i].size}`);
|
||||
};
|
||||
reader.readAsDataURL(files[i]);
|
||||
foundFile = true
|
||||
break
|
||||
}
|
||||
} else if (data.type == "message") {
|
||||
console.log(`[info] ${data.message}`);
|
||||
} else {
|
||||
console.log(`[debug] unknown`);
|
||||
}
|
||||
};
|
||||
const socketOpenListener = (event) => {
|
||||
console.log('[debug] connected');
|
||||
};
|
||||
if (foundFile == false) {
|
||||
socketSend({
|
||||
type: "get",
|
||||
message: "not found",
|
||||
success: false,
|
||||
})
|
||||
consoleLog(`[info] ${data.message} 404`);
|
||||
}
|
||||
} else if (data.type == "message") {
|
||||
consoleLog(`[info] ${data.message}`);
|
||||
} else {
|
||||
consoleLog(`[debug] unknown`);
|
||||
}
|
||||
};
|
||||
const socketOpenListener = (event) => {
|
||||
consoleLog('[info] connected');
|
||||
};
|
||||
|
||||
const socketCloseListener = (event) => {
|
||||
if (socket) {
|
||||
console.log('[debug] disconnected');
|
||||
}
|
||||
var url = window.origin.replace("http", "ws") + '/ws';
|
||||
try {
|
||||
socket = new WebSocket(url);
|
||||
socket.addEventListener('open', socketOpenListener);
|
||||
socket.addEventListener('message', socketMessageListener);
|
||||
socket.addEventListener('close', socketCloseListener);
|
||||
} catch (err) {
|
||||
console.log("[debug] no connection available")
|
||||
}
|
||||
};
|
||||
const socketCloseListener = (event) => {
|
||||
if (socket) {
|
||||
consoleLog('[info] disconnected');
|
||||
}
|
||||
var url = window.origin.replace("http", "ws") + '/ws';
|
||||
try {
|
||||
socket = new WebSocket(url);
|
||||
socket.addEventListener('open', socketOpenListener);
|
||||
socket.addEventListener('message', socketMessageListener);
|
||||
socket.addEventListener('close', socketCloseListener);
|
||||
} catch (err) {
|
||||
consoleLog("[info] no connection available")
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
socketCloseListener();
|
||||
socketCloseListener();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
|
|
|||
91
utils.go
Normal file
91
utils.go
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// GetClientIPHelper gets the client IP using a mixture of techniques.
|
||||
// This is how it is with golang at the moment.
|
||||
func GetClientIPHelper(req *http.Request) (ipResult string, errResult error) {
|
||||
|
||||
// Try lots of ways :) Order is important.
|
||||
// Try Request Headers (X-Forwarder). Client could be behind a Proxy
|
||||
ip, err := getClientIPByHeaders(req)
|
||||
if err == nil {
|
||||
log.Printf("debug: Found IP using Request Headers sniffing. ip: %v", ip)
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
// Try by Request
|
||||
ip, err = getClientIPByRequestRemoteAddr(req)
|
||||
if err == nil {
|
||||
log.Printf("debug: Found IP using Request sniffing. ip: %v", ip)
|
||||
return ip, nil
|
||||
}
|
||||
|
||||
// Try Request Header ("Origin")
|
||||
url, err := url.Parse(req.Header.Get("Origin"))
|
||||
if err == nil {
|
||||
host := url.Host
|
||||
ip, _, err := net.SplitHostPort(host)
|
||||
if err == nil {
|
||||
log.Printf("debug: Found IP using Header (Origin) sniffing. ip: %v", ip)
|
||||
return ip, nil
|
||||
}
|
||||
}
|
||||
|
||||
err = errors.New("error: Could not find clients IP address")
|
||||
return "", err
|
||||
}
|
||||
|
||||
// getClientIPByRequest tries to get directly from the Request.
|
||||
// https://blog.golang.org/context/userip/userip.go
|
||||
func getClientIPByRequestRemoteAddr(req *http.Request) (ip string, err error) {
|
||||
|
||||
// Try via request
|
||||
ip, port, err := net.SplitHostPort(req.RemoteAddr)
|
||||
if err != nil {
|
||||
log.Printf("debug: Getting req.RemoteAddr %v", err)
|
||||
return "", err
|
||||
} else {
|
||||
log.Printf("debug: With req.RemoteAddr found IP:%v; Port: %v", ip, port)
|
||||
}
|
||||
|
||||
userIP := net.ParseIP(ip)
|
||||
if userIP == nil {
|
||||
message := fmt.Sprintf("debug: Parsing IP from Request.RemoteAddr got nothing.")
|
||||
log.Printf(message)
|
||||
return "", fmt.Errorf(message)
|
||||
|
||||
}
|
||||
log.Printf("debug: Found IP: %v", userIP)
|
||||
return userIP.String(), nil
|
||||
|
||||
}
|
||||
|
||||
// getClientIPByHeaders tries to get directly from the Request Headers.
|
||||
// This is only way when the client is behind a Proxy.
|
||||
func getClientIPByHeaders(req *http.Request) (ip string, err error) {
|
||||
|
||||
// Client could be behid a Proxy, so Try Request Headers (X-Forwarder)
|
||||
ipSlice := []string{}
|
||||
|
||||
ipSlice = append(ipSlice, req.Header.Get("X-Forwarded-For"))
|
||||
ipSlice = append(ipSlice, req.Header.Get("x-forwarded-for"))
|
||||
ipSlice = append(ipSlice, req.Header.Get("X-FORWARDED-FOR"))
|
||||
|
||||
for _, v := range ipSlice {
|
||||
log.Printf("debug: client request header check gives ip: %v", v)
|
||||
if v != "" {
|
||||
return v, nil
|
||||
}
|
||||
}
|
||||
err = errors.New("error: Could not find clients IP address from the Request Headers")
|
||||
return "", err
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue