Skip to content

Commit

Permalink
Merge pull request #36 from patrickhener/restructure
Browse files Browse the repository at this point in the history
Merge to main
  • Loading branch information
patrickhener authored Feb 28, 2023
2 parents bd64fa3 + f8a6d6c commit 6574dda
Show file tree
Hide file tree
Showing 65 changed files with 1,183 additions and 863 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
# uglify-js and https://github.com/wellington/wellington needed
generate:
@echo "[*] Minifying and compiling scss and js"
@uglifyjs -o internal/myhttp/static/js/main.min.js assets/js/main.js
@wt compile assets/css/style.scss -s compressed -b internal/myhttp/static/css
@uglifyjs -o httpserver/static/js/main.min.js assets/js/main.js
@wt compile assets/css/style.scss -s compressed -b httpserver/static/css
@echo "[OK] Done minifying and compiling things"

security:
Expand Down
72 changes: 66 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![Version](https://img.shields.io/badge/Version-v0.2.0-green)
![Version](https://img.shields.io/badge/Version-v0.3.0-green)
[![GitHub](https://img.shields.io/github/license/patrickhener/goshs)](https://github.com/patrickhener/goshs/blob/master/LICENSE)
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/patrickhener/goshs)
[![GitHub issues](https://img.shields.io/github/issues-raw/patrickhener/goshs)](https://github.com/patrickhener/goshs/issues)
Expand All @@ -23,6 +23,8 @@ goshs is a replacement for Python's `SimpleHTTPServer`. It allows uploading and
* Download clipboard entries as .json file
* WebDAV support
* Read-Only and Upload-Only mode
* Silent mode (no webserver output)
* Retrieve json on cli

# Installation

Expand All @@ -33,7 +35,7 @@ You can download the executable from the [release section](https://github.com/pa

```bash
go get -u github.com/patrickhener/goshs
go install github.com/patrickhener/goshs
go install github.com/patrickhener/goshs@latest
```

## Build yourself
Expand Down Expand Up @@ -123,10 +125,68 @@ This mode will omit the dir listing on the web interface. Also you will not have

`goshs -si`

# Credits
**Retrieve the directory listing in json format**
You can now retrieve the directory listing in *json* format. This is meant to be used with curl for example in environments where you do not have a browser on hand.
```bash
curl -s localhost:8000/?json | jq
[
{
"name": ".git/",
"is_dir": true,
"is_symlink": false,
"symlink_target": "",
"extension": "",
"size_bytes": 4096,
"last_modified": "2023-02-28T15:38:11.982+01:00"
},
{
"name": ".github/",
"is_dir": true,
"is_symlink": false,
"symlink_target": "",
"extension": "",
"size_bytes": 4096,
"last_modified": "2023-02-28T10:27:35.524+01:00"
},
{
"name": ".gitignore",
"is_dir": false,
"is_symlink": false,
"symlink_target": "",
"extension": ".gitignore",
"size_bytes": 48,
"last_modified": "2023-02-20T07:58:46.436+01:00"
},
... snip ...
```
Or with path:
A special thank you goes to *sc0tfree* for inspiring this project with his project [updog](https://github.com/sc0tfree/updog) written in Python.
```bash
curl -s localhost:8000/utils?json | jq
[
{
"name": "utils.go",
"is_dir": false,
"is_symlink": false,
"symlink_target": "",
"extension": ".go",
"size_bytes": 2218,
"last_modified": "2023-02-28T15:28:54.783+01:00"
},
{
"name": "utils_test.go",
"is_dir": false,
"is_symlink": false,
"symlink_target": "",
"extension": ".go",
"size_bytes": 2012,
"last_modified": "2023-02-28T15:28:12.748+01:00"
}
]
```
# Tutorial Series
I wrote several blog posts how and why I implemented all of this. You can find it [here](https://hesec.de/tags/goshs/) if you are interested about the technical background.
# Credits
A special thank you goes to *sc0tfree* for inspiring this project with his project [updog](https://github.com/sc0tfree/updog) written in Python.
4 changes: 2 additions & 2 deletions assets/css/font.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
/* Fira Code VF from 300 to 700 */
@font-face {
font-family: 'Fira Code VF';
src: url('../fonts/FiraCode-VF.woff2') format('woff2-variations'),
url('../fonts/FiraCode-VF.woff') format('woff-variations');
src: url('../fonts/FiraCode-VF.woff2?static') format('woff2-variations'),
url('../fonts/FiraCode-VF.woff?static') format('woff-variations');
font-weight: 300 700;
font-style: normal;
}
11 changes: 2 additions & 9 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,10 @@ function selectNone() {
document.getElementById('downloadBulkButton').style.display = 'none';
}

// Everything related to websockets
var wsURL = '';
location.protocol !== 'https:'
? (wsURL =
'ws://' +
window.location.host +
'/14644be038ea0118a1aadfacca2a7d1517d7b209c4b9674ee893b1944d1c2d54/ws')
: (wsURL =
'wss://' +
window.location.host +
'/14644be038ea0118a1aadfacca2a7d1517d7b209c4b9674ee893b1944d1c2d54/ws');
? (wsURL = 'ws://' + window.location.host + '/?ws')
: (wsURL = 'wss://' + window.location.host + '/?ws');
var connection = new WebSocket(wsURL);

connection.onopen = function () {
Expand Down
28 changes: 14 additions & 14 deletions internal/myca/ca.go → ca/ca.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Package myca ...
// Package ca will handle the creation of certificates for TSL encrypted communication
// Credits: Shane Utt
// https://shaneutt.com/blog/golang-ca-and-signed-cert-go/
package myca
package ca

import (
"bytes"
"crypto/rand"
"crypto/rsa"
"os"

// disable G505 (CWE-327): Blocklisted import crypto/sha1: weak cryptographic primitive
// #nosec G505
Expand All @@ -17,13 +18,12 @@ import (
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"io/ioutil"
"net"
"strings"
"time"

"github.com/patrickhener/goshs/internal/mylog"
"github.com/patrickhener/goshs/internal/myutils"
"github.com/patrickhener/goshs/logger"
"github.com/patrickhener/goshs/utils"
)

// Sum will give the sha256 and sha1 sum of the certificate
Expand Down Expand Up @@ -70,7 +70,7 @@ func ParseAndSum(cert string) (sha256s, sha1s string, err error) {
// disable G304 (CWE-22): Potential file inclusion via variable
// risk accepted, maybe check if can be used to do malicous things
// #nosec G304
certBytes, err := ioutil.ReadFile(cert)
certBytes, err := os.ReadFile(cert)
if err != nil {
return "", "", err
}
Expand All @@ -89,9 +89,9 @@ func ParseAndSum(cert string) (sha256s, sha1s string, err error) {

// Setup will deliver a fully initialized CA and server cert
func Setup() (serverTLSConf *tls.Config, sha256s, sha1s string, err error) {
randInt, err := myutils.RandomNumber()
randInt, err := utils.RandomNumber()
if err != nil {
mylog.Errorf("when creating certificate: %+v", err)
logger.Errorf("when creating certificate: %+v", err)
}
ca := &x509.Certificate{
SerialNumber: &randInt,
Expand Down Expand Up @@ -131,20 +131,20 @@ func Setup() (serverTLSConf *tls.Config, sha256s, sha1s string, err error) {
Type: "CERTIFICATE",
Bytes: caBytes,
}); err != nil {
mylog.Errorf("encoding pem: %+v", err)
logger.Errorf("encoding pem: %+v", err)
}

caPrivKeyPEM := new(bytes.Buffer)
if err := pem.Encode(caPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(caPrivKey),
}); err != nil {
mylog.Errorf("encoding pem: %+v", err)
logger.Errorf("encoding pem: %+v", err)
}

randInt, err = myutils.RandomNumber()
randInt, err = utils.RandomNumber()
if err != nil {
mylog.Errorf("when creating certificate: %+v", err)
logger.Errorf("when creating certificate: %+v", err)
}
// set up our server certificate
cert := &x509.Certificate{
Expand Down Expand Up @@ -182,15 +182,15 @@ func Setup() (serverTLSConf *tls.Config, sha256s, sha1s string, err error) {
Type: "CERTIFICATE",
Bytes: certBytes,
}); err != nil {
mylog.Errorf("encoding pem: %+v", err)
logger.Errorf("encoding pem: %+v", err)
}

certPrivKeyPEM := new(bytes.Buffer)
if err := pem.Encode(certPrivKeyPEM, &pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
}); err != nil {
mylog.Errorf("encoding pem: %+v", err)
logger.Errorf("encoding pem: %+v", err)
}

serverCert, err := tls.X509KeyPair(certPEM.Bytes(), certPrivKeyPEM.Bytes())
Expand Down
56 changes: 56 additions & 0 deletions ca/ca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package ca

import (
"strings"
"testing"
)

const (
certSha256 string = "65853DC369E138125B42FCE21DFF13CD93B5A0E3D2EB61107EF3378106759940"
certSha1 string = "AB51F4B4D3336129576C5CA46408A6A79EA62FB2"
)

var (
cert []byte = []byte(`BEGIN CERTI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hbXBsZS5jb22CD3d3dy5leGFtcGxlLm5ldIcECgAAATAN
BgkqhkiG9w0BAQsFAAOCAgEAY0BOskg82Ty//ADLyiXVhwEV/lLILH0BWWWVv05i
frrUs2fA3ORkUoRaiJxAzDvEV98AjD52Ty6WYtgNscmrQxxz0gn3xUWZXdj6L5PE
C6xj4lH0M/bNW6TUWhBqaVsaUCaoEhze4ieTcUHZlF61tkscdfUf8cIwi4vyNA3v
LIzXgJxIYw+wD5K1MEtVOPX3x9oO2Ceu3dLQv7MWULVjir1Pm2/3YhfGJgttFNHu
fbFWrPRG/m1MiMniGxQb3Oa1IjPZU70elP3GGG+irojxWcFYW+MopeXelVxbC63I
z7uc7cbmmcsD2GIkv1td4pe84UYy+pCsKQ8vXykwnjcFfjkrPSxdz1tVjrz4V60e
jVekhGgIHQAm/PyLLssQDykiX6ySxurU8bCcpqaA6dBzlUEX+Ym9xr2l155U6OsP
k4HWPGqf2/xMaXq/7g9HfqPhj9tZ/x2wyZ6Mx2rMtRV+6hX1XGa4tZU0f5MziEnX
Lkf4fEY4kc28UdEZRiG+D8cK6k0N8fHkKo4M09f+PJp/4sqa2g2kj8aqEFzSu2uq
v/cKeJ20txB/Egu9OGCS3aFqQ9zV+rqkEko2agLbqY/Aks3e/jaAIwURvxNS83l4
rJ90pXPe6awWutwMfmwlzqv0UYLu0IGHZiN8uTPiQ0nkR2kps3MruAsj1K9PaNpq
rtI=
-----END CERTIFICATE-----`)
)

func TestSum(t *testing.T) {

sha256, sha1 := Sum(cert)
sha256Clean := strings.ReplaceAll(sha256, " ", "")
sha1Clean := strings.ReplaceAll(sha1, " ", "")

if sha256Clean != certSha256 || sha1Clean != certSha1 {
t.Errorf("Certificate Fingerprint was wrong: got sha256 %s sha1 %s, want sha256 %s and sha1 %s", sha256Clean, sha1Clean, certSha256, certSha1)
}
}
6 changes: 2 additions & 4 deletions internal/myclipboard/clipboard.go → clipboard/clipboard.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package myclipboard
// Package clipboard will provide the functionality of a clipboard
package clipboard

import (
"encoding/json"
"time"

"github.com/patrickhener/goshs/internal/mylog"
)

// Clipboard is the in memory clipboard to hold the copy-pasteable content
Expand Down Expand Up @@ -81,7 +80,6 @@ func (c *Clipboard) Download() ([]byte, error) {
func reindex(entries []Entry) []Entry {
var newEntries []Entry
for i, e := range entries {
mylog.Debugf("Entry #%d: %+v\n", i, e)
newEntries = append(newEntries, Entry{
ID: i,
Content: e.Content,
Expand Down
Loading

0 comments on commit 6574dda

Please sign in to comment.