-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
151 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/charmbracelet/soft/config" | ||
appCfg "github.com/charmbracelet/soft/internal/config" | ||
"github.com/charmbracelet/wish/git" | ||
"goji.io" | ||
"goji.io/pat" | ||
"goji.io/pattern" | ||
) | ||
|
||
type HTTPServer struct { | ||
server *http.Server | ||
gitHandler http.Handler | ||
cfg *config.Config | ||
ac *appCfg.Config | ||
} | ||
|
||
func NewHTTPServer(cfg *config.Config, ac *appCfg.Config) *HTTPServer { | ||
h := goji.NewMux() | ||
s := &HTTPServer{ | ||
cfg: cfg, | ||
ac: ac, | ||
gitHandler: http.FileServer(http.Dir(cfg.RepoPath)), | ||
server: &http.Server{ | ||
Addr: fmt.Sprintf(":%d", cfg.HTTPPort), | ||
Handler: h, | ||
TLSConfig: cfg.TLSConfig, | ||
}, | ||
} | ||
h.HandleFunc(pat.Get("/:repo"), s.handleGit) | ||
h.HandleFunc(pat.Get("/:repo/*"), s.handleGit) | ||
return s | ||
} | ||
|
||
func (s *HTTPServer) Start() error { | ||
if s.cfg.HTTPScheme == "https" { | ||
return s.server.ListenAndServeTLS("", "") | ||
} else { | ||
return s.server.ListenAndServe() | ||
} | ||
} | ||
|
||
func (s *HTTPServer) handleGit(w http.ResponseWriter, r *http.Request) { | ||
repo := pat.Param(r, "repo") | ||
access := s.ac.AuthRepo(repo, nil) | ||
if access < git.ReadOnlyAccess || !s.ac.AllowKeyless { | ||
http.Error(w, "Unauthorized", http.StatusUnauthorized) | ||
return | ||
} | ||
path := pattern.Path(r.Context()) | ||
stat, err := os.Stat(filepath.Join(s.cfg.RepoPath, repo, path)) | ||
// Restrict access to files | ||
if err != nil || stat.IsDir() { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
w.Header().Set("Content-Type", "text/plain; charset=utf-8") | ||
w.Header().Set("X-Content-Type-Options", "nosniff") | ||
r.URL.Path = fmt.Sprintf("/%s/%s", repo, path) | ||
s.gitHandler.ServeHTTP(w, r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/charmbracelet/soft/config" | ||
appCfg "github.com/charmbracelet/soft/internal/config" | ||
"github.com/charmbracelet/soft/internal/tui" | ||
"github.com/charmbracelet/wish" | ||
bm "github.com/charmbracelet/wish/bubbletea" | ||
gm "github.com/charmbracelet/wish/git" | ||
lm "github.com/charmbracelet/wish/logging" | ||
"github.com/gliderlabs/ssh" | ||
) | ||
|
||
type SSHServer struct { | ||
s *ssh.Server | ||
cfg *config.Config | ||
ac *appCfg.Config | ||
} | ||
|
||
// NewSSHServer returns a new *ssh.Server configured to serve Soft Serve. The SSH | ||
// server key-pair will be created if none exists. An initial admin SSH public | ||
// key can be provided with authKey. If authKey is provided, access will be | ||
// restricted to that key. If authKey is not provided, the server will be | ||
// publicly writable until configured otherwise by cloning the `config` repo. | ||
func NewSSHServer(cfg *config.Config, ac *appCfg.Config) *SSHServer { | ||
mw := []wish.Middleware{ | ||
bm.Middleware(tui.SessionHandler(ac)), | ||
gm.Middleware(cfg.RepoPath, ac), | ||
lm.Middleware(), | ||
} | ||
s, err := wish.NewServer( | ||
ssh.PublicKeyAuth(ac.PublicKeyHandler), | ||
ssh.PasswordAuth(ac.PasswordHandler), | ||
wish.WithAddress(fmt.Sprintf("%s:%d", cfg.Host, cfg.SSHPort)), | ||
wish.WithHostKeyPath(cfg.KeyPath), | ||
wish.WithMiddleware(mw...), | ||
) | ||
if err != nil { | ||
log.Fatalln(err) | ||
} | ||
return &SSHServer{ | ||
s: s, | ||
cfg: cfg, | ||
ac: ac, | ||
} | ||
} | ||
|
||
func (s *SSHServer) Start() error { | ||
return s.s.ListenAndServe() | ||
} |