refactor: project directory structure

This commit is contained in:
0xJacky 2023-11-26 18:59:12 +08:00
parent c1193a5b8c
commit e5a5889931
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
367 changed files with 710 additions and 756 deletions

View file

@ -0,0 +1,90 @@
package nginx
import (
"bufio"
"fmt"
"github.com/tufanbarisyildirim/gonginx"
"github.com/tufanbarisyildirim/gonginx/parser"
"strings"
)
func buildComments(orig string, indent int) (content string) {
scanner := bufio.NewScanner(strings.NewReader(orig))
for scanner.Scan() {
content += strings.Repeat("\t", indent) + "# " + strings.TrimSpace(scanner.Text()) + "\n"
}
content = strings.TrimLeft(content, "\n")
return
}
func (c *NgxConfig) BuildConfig() (content string) {
// Custom
if c.Custom != "" {
content += c.Custom
content += "\n\n"
}
// Upstreams
for _, u := range c.Upstreams {
upstream := ""
var comments string
for _, directive := range u.Directives {
if directive.Comments != "" {
comments = buildComments(directive.Comments, 1)
}
upstream += fmt.Sprintf("%s\t%s;\n", comments, directive.Orig())
}
comments = buildComments(u.Comments, 1)
content += fmt.Sprintf("upstream %s {\n%s%s}\n\n", u.Name, comments, upstream)
}
// Servers
for _, s := range c.Servers {
server := ""
// directives
for _, directive := range s.Directives {
var comments string
if directive.Comments != "" {
comments = buildComments(directive.Comments, 1)
}
if directive.Params != "" {
server += fmt.Sprintf("%s\t%s;\n", comments, directive.Orig())
}
}
if len(s.Directives) > 0 {
server += "\n"
}
// locations
locations := ""
for _, location := range s.Locations {
locationContent := ""
scanner := bufio.NewScanner(strings.NewReader(location.Content))
for scanner.Scan() {
locationContent += "\t\t" + scanner.Text() + "\n"
}
var comments string
if location.Comments != "" {
comments = buildComments(location.Comments, 1)
}
locations += fmt.Sprintf("%s\tlocation %s {\n%s\t}\n\n", comments, location.Path, locationContent)
}
server += locations
var comments string
if s.Comments != "" {
comments = buildComments(s.Comments, 0) + "\n"
}
content += fmt.Sprintf("%sserver {\n%s}\n\n", comments, server)
}
p := parser.NewStringParser(content)
config := p.Parse()
content = gonginx.DumpConfig(config, gonginx.IndentedStyle)
return
}

View file

@ -0,0 +1,83 @@
# this is a comments
upstream my-api {
server 127.0.0.1:9001;
server 127.0.0.1:9002;
}
# this is a comments
server {
# this is a comments
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name cloud.jackyu.cn;
# this is a comments
ssl_certificate /etc/nginx/ssl/jackyu.cn/alpha/jackyu.cn_server_cert.pem;
ssl_certificate_key /etc/nginx/ssl/jackyu.cn/alpha/jackyu.cn_key.pem;
fastcgi_hide_header X-Powered-By; # Remove X-Powered-By, which is an information leak
if ($invalid_referer) {
return 403;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Make a regex exception for `/.well-known` so that clients can still
# access it despite the existence of the regex rule
# `location ~ /(\.|autotest|...)` which would otherwise handle requests
# for `/.well-known`.
location = /.well-known/carddav { return 301 /remote.php/dav/; }
location ^~ /.well-known
{
# The rules in this block are an adaptation of the rules
# in `.htaccess` that concern `/.well-known`.
location = /.well-known/carddav { return 301 /remote.php/dav/; }
location = /.well-known/caldav { return 301 /remote.php/dav/; }
location /.well-known/acme-challenge { try_files $uri $uri/ =404; }
location /.well-known/pki-validation {
try_files $uri $uri/ =404;
}
# Let Nextcloud's API for `/.well-known` URIs handle all other
# requests by passing them to the front-end controller.
return 301 /index.php$request_uri;
}
# set max upload size
client_max_body_size 8192M;
fastcgi_buffers 64 4K;
# Enable gzip but do not remove ETag headers
gzip on; gzip_vary on; location /x/ {}gzip_comp_level 4;
gzip_min_length 256;gzip_proxied expired no-cache no-store private no_last_modified no_etag auth;
gzip_types application/atom+xml application/javascript application/json application/ld+json application/manifest+json application/rss+xml application/vnd.geo+json application/vnd.ms-fontobject application/x-font-ttf application/x-web-app-manifest+json application/xhtml+xml application/xml font/opentype image/bmp image/svg+xml image/x-icon text/cache-manifest text/css text/plain text/vcard text/vnd.rim.location.xloc text/vtt text/x-component text/x-cross-domain-policy;
# Uncomment if your server is build with the ngx_pagespeed module
# This module is currently not supported.
# pagespeed off;
location / {
if ( $http_user_agent ~ ^DavClnt ) {
return 302 /remote.php/webdav/$is_args$args;
}
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_http_version 1.1;
proxy_intercept_errors on;
proxy_pass http://172.17.0.1:7000;
}
}

View file

@ -0,0 +1,37 @@
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
listen [::]:80;
server_name blog.jackyu.cn test.jackyu.cn;
location /.well-known/acme-challenge {
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
proxy_pass http://127.0.0.1:9180;
}
}
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name blog.jackyu.cn test.jackyu.cn;
ssl_certificate /etc/nginx/ssl/blog.jackyu.cn_test.jackyu.cn/fullchain.cer;
ssl_certificate_key /etc/nginx/ssl/blog.jackyu.cn_test.jackyu.cn/private.key;
include enable-php-8.conf;
location /.well-known/acme-challenge {
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr:$remote_port;
proxy_pass http://127.0.0.1:9180;
}
}

View file

@ -0,0 +1,18 @@
package nginx
import (
"github.com/tufanbarisyildirim/gonginx"
"github.com/tufanbarisyildirim/gonginx/parser"
)
func (c *NgxConfig) FmtCode() (fmtContent string) {
fmtContent = gonginx.DumpConfig(c.c, gonginx.IndentedStyle)
return
}
func FmtCode(content string) (fmtContent string) {
p := parser.NewStringParser(content)
c := p.Parse()
fmtContent = gonginx.DumpConfig(c, gonginx.IndentedStyle)
return
}

30
internal/nginx/log.go Normal file
View file

@ -0,0 +1,30 @@
package nginx
import "strings"
// refer to https://nginx.org/en/docs/ngx_core_module.html#error_log
// nginx log level: debug, info, notice, warn, error, crit, alert, or emerg
const (
Debug = iota
Info
Notice
Warn
Error
Crit
Alert
Emerg
)
var logLevel = [...]string{
"debug", "info", "notice", "warn", "error", "crit", "alert", "emerg",
}
func GetLogLevel(output string) int {
for k, v := range logLevel {
if strings.Contains(output, v) {
return k
}
}
return -1
}

99
internal/nginx/nginx.go Normal file
View file

@ -0,0 +1,99 @@
package nginx
import (
"github.com/0xJacky/Nginx-UI/internal/logger"
"github.com/0xJacky/Nginx-UI/settings"
"os/exec"
"path/filepath"
"regexp"
)
func execShell(cmd string) (out string) {
bytes, err := exec.Command("/bin/sh -c", cmd).CombinedOutput()
out = string(bytes)
if err != nil {
out += " " + err.Error()
}
return
}
func TestConf() (out string) {
if settings.NginxSettings.TestConfigCmd != "" {
out = execShell(settings.NginxSettings.TestConfigCmd)
return
}
out = execShell("nginx -t")
return
}
func Reload() (out string) {
if settings.NginxSettings.ReloadCmd != "" {
out = execShell(settings.NginxSettings.ReloadCmd)
return
}
out = execShell("nginx -s reload")
return
}
func Restart() (out string) {
if settings.NginxSettings.RestartCmd != "" {
out = execShell(settings.NginxSettings.RestartCmd)
return
}
out = execShell("nginx -s reopen")
return
}
func GetConfPath(dir ...string) string {
var confPath string
if settings.NginxSettings.ConfigDir == "" {
out, err := exec.Command("nginx", "-V").CombinedOutput()
if err != nil {
logger.Error(err)
return ""
}
r, _ := regexp.Compile("--conf-path=(.*)/(.*.conf)")
match := r.FindStringSubmatch(string(out))
if len(match) < 1 {
logger.Error("nginx.GetConfPath len(match) < 1")
return ""
}
confPath = r.FindStringSubmatch(string(out))[1]
} else {
confPath = settings.NginxSettings.ConfigDir
}
return filepath.Join(confPath, filepath.Join(dir...))
}
func GetNginxPIDPath() string {
var confPath string
if settings.NginxSettings.PIDPath == "" {
out, err := exec.Command("nginx", "-V").CombinedOutput()
if err != nil {
logger.Error(err)
return ""
}
r, _ := regexp.Compile("--pid-path=(.*.pid)")
match := r.FindStringSubmatch(string(out))
if len(match) < 1 {
logger.Error("nginx.GetNginxPIDPath len(match) < 1")
return ""
}
confPath = r.FindStringSubmatch(string(out))[1]
} else {
confPath = settings.NginxSettings.PIDPath
}
return confPath
}

View file

@ -0,0 +1,49 @@
package nginx
import (
"fmt"
"github.com/tufanbarisyildirim/gonginx"
"github.com/tufanbarisyildirim/gonginx/parser"
"strings"
"testing"
)
func TestNgxConfParse(t *testing.T) {
p, err := parser.NewParser("conf/nextcloud_ngx.conf")
if err != nil {
fmt.Println(err)
return
}
n := p.Parse()
fn(n.Block, 0)
c, err := ParseNgxConfig("conf/nextcloud_ngx.conf")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(c)
c, err = ParseNgxConfig("conf/test.conf")
if err != nil {
fmt.Println(err)
return
}
fmt.Println(c)
}
func fn(block gonginx.IBlock, deep int) {
if block == nil {
return
}
for _, v := range block.GetDirectives() {
if len(v.GetComment()) > 0 {
for _, c := range v.GetComment() {
fmt.Println(strings.Repeat("\t", deep), c)
}
}
fmt.Println(fmt.Sprintf("%s%s %s", strings.Repeat("\t", deep), v.GetName(), strings.Join(v.GetParameters(), " ")))
fn(v.GetBlock(), deep+1)
}
}

177
internal/nginx/parse.go Normal file
View file

@ -0,0 +1,177 @@
package nginx
import (
"github.com/pkg/errors"
"github.com/tufanbarisyildirim/gonginx"
"github.com/tufanbarisyildirim/gonginx/parser"
"strings"
)
const (
Server = "server"
Location = "location"
Upstream = "upstream"
)
func (s *NgxServer) ParseServer(directive gonginx.IDirective) {
s.parseServer(directive)
}
func (s *NgxServer) parseServer(directive gonginx.IDirective) {
if directive.GetBlock() == nil {
return
}
for _, d := range directive.GetBlock().GetDirectives() {
switch d.GetName() {
case Location:
location := &NgxLocation{
Path: strings.Join(d.GetParameters(), " "),
Comments: buildComment(d.GetComment()),
}
location.parseLocation(d, 0)
s.Locations = append(s.Locations, location)
default:
dir := &NgxDirective{
Directive: d.GetName(),
Comments: buildComment(d.GetComment()),
}
dir.parseDirective(d, 0)
s.Directives = append(s.Directives, dir)
}
}
}
func (l *NgxLocation) ParseLocation(directive gonginx.IDirective, deep int) {
l.parseLocation(directive, deep)
}
func (l *NgxLocation) parseLocation(directive gonginx.IDirective, deep int) {
if directive.GetBlock() == nil {
return
}
for _, location := range directive.GetBlock().GetDirectives() {
if len(location.GetComment()) > 0 {
for _, c := range location.GetComment() {
l.Content += strings.Repeat("\t", deep) + c + "\n"
}
}
l.Content += strings.Repeat("\t", deep) + location.GetName() + " " + strings.Join(location.GetParameters(), " ")
if location.GetBlock() != nil && location.GetBlock().GetDirectives() != nil {
l.Content += " { \n"
l.parseLocation(location, deep+1)
l.Content += " } \n"
} else {
l.Content += ";\n"
}
}
}
func (d *NgxDirective) ParseDirective(directive gonginx.IDirective, deep int) {
d.parseDirective(directive, deep)
}
func (d *NgxDirective) parseDirective(directive gonginx.IDirective, deep int) {
if directive.GetBlock() != nil {
d.Params += directive.GetName() + " "
d.Directive = ""
}
d.Params += strings.Join(directive.GetParameters(), " ")
if directive.GetBlock() != nil {
d.Params += " {\n"
for _, location := range directive.GetBlock().GetDirectives() {
if len(location.GetComment()) > 0 {
for _, c := range location.GetComment() {
d.Params += strings.Repeat("\t", deep) + c + "\n"
}
}
d.Params += strings.Repeat("\t", deep+1) + location.GetName() + " " +
strings.Join(location.GetParameters(), " ") + ";\n"
// d.parseDirective(location, deep+1)
if location.GetBlock() == nil {
continue
}
for _, v := range location.GetBlock().GetDirectives() {
d.parseDirective(v, deep+1)
}
}
d.Params += "}\n"
return
}
}
func (u *NgxUpstream) parseUpstream(directive gonginx.IDirective) {
if directive.GetBlock() == nil {
return
}
for _, us := range directive.GetBlock().GetDirectives() {
d := &NgxDirective{
Directive: us.GetName(),
Params: strings.Join(us.GetParameters(), " "),
Comments: buildComment(us.GetComment()),
}
u.Directives = append(u.Directives, d)
}
}
func (c *NgxConfig) parseCustom(directive gonginx.IDirective) {
if directive.GetBlock() == nil {
return
}
c.Custom += "{\n"
for _, v := range directive.GetBlock().GetDirectives() {
c.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
v.GetName() + " " + strings.Join(v.GetParameters(), " ") + ";\n"
}
c.Custom += "}\n"
}
func buildComment(c []string) string {
return strings.ReplaceAll(strings.Join(c, "\n"), "#", "")
}
func parse(block gonginx.IBlock, ngxConfig *NgxConfig) {
if block == nil {
return
}
for _, v := range block.GetDirectives() {
comments := buildComment(v.GetComment())
switch v.GetName() {
case Server:
server := NewNgxServer()
server.Comments = comments
server.parseServer(v)
ngxConfig.Servers = append(ngxConfig.Servers, server)
case Upstream:
upstream := &NgxUpstream{
Name: strings.Join(v.GetParameters(), " "),
}
upstream.Comments = comments
upstream.parseUpstream(v)
ngxConfig.Upstreams = append(ngxConfig.Upstreams, upstream)
default:
ngxConfig.Custom += strings.Join(v.GetComment(), "\n") + "\n" +
v.GetName() + " " + strings.Join(v.GetParameters(), " ") + "\n"
ngxConfig.parseCustom(v)
}
}
ngxConfig.Custom = FmtCode(ngxConfig.Custom)
}
func ParseNgxConfigByContent(content string) (ngxConfig *NgxConfig) {
p := parser.NewStringParser(content)
c := p.Parse()
ngxConfig = NewNgxConfig("")
ngxConfig.c = c
parse(c.Block, ngxConfig)
return
}
func ParseNgxConfig(filename string) (ngxConfig *NgxConfig, err error) {
p, err := parser.NewParser(filename)
if err != nil {
return nil, errors.Wrap(err, "error ParseNgxConfig")
}
c := p.Parse()
ngxConfig = NewNgxConfig(filename)
ngxConfig.c = c
parse(c.Block, ngxConfig)
return
}

64
internal/nginx/type.go Normal file
View file

@ -0,0 +1,64 @@
package nginx
import (
"github.com/tufanbarisyildirim/gonginx"
"path"
"strings"
)
type NgxConfig struct {
FileName string `json:"file_name"`
Name string `json:"name"`
Upstreams []*NgxUpstream `json:"upstreams"`
Servers []*NgxServer `json:"servers"`
Custom string `json:"custom"`
c *gonginx.Config
}
type NgxServer struct {
Directives []*NgxDirective `json:"directives"`
Locations []*NgxLocation `json:"locations"`
Comments string `json:"comments"`
}
type NgxUpstream struct {
Name string `json:"name"`
Directives []*NgxDirective `json:"directives"`
Comments string `json:"comments"`
}
type NgxDirective struct {
Directive string `json:"directive"`
Params string `json:"params"`
Comments string `json:"comments"`
}
type NgxLocation struct {
Path string `json:"path"`
Content string `json:"content"`
Comments string `json:"comments"`
}
func (d *NgxDirective) Orig() string {
return d.Directive + " " + d.Params
}
func (d *NgxDirective) TrimParams() {
d.Params = strings.TrimRight(strings.TrimSpace(d.Params), ";")
return
}
func NewNgxServer() *NgxServer {
return &NgxServer{
Locations: make([]*NgxLocation, 0),
Directives: make([]*NgxDirective, 0),
}
}
func NewNgxConfig(filename string) *NgxConfig {
return &NgxConfig{
FileName: filename,
Upstreams: make([]*NgxUpstream, 0),
Name: path.Base(filename),
}
}