Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
caddyproxy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
vicotor
caddyproxy
Commits
5532c088
Commit
5532c088
authored
Sep 30, 2024
by
vicotor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add function for forward
parent
cc502407
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
134 additions
and
4 deletions
+134
-4
caddyapi.go
caddy/caddyapi.go
+67
-4
template.go
caddy/template.go
+57
-0
type.go
caddy/type.go
+10
-0
No files found.
caddy/caddyapi.go
View file @
5532c088
...
...
@@ -43,12 +43,12 @@ func (c *CaddyAPI) buildPayloadForCreateWebsite(domain string, root string) (str
return
buffer
.
String
(),
nil
}
func
(
c
*
CaddyAPI
)
newCaddyFile
(
domain
string
,
root
string
)
error
{
func
(
c
*
CaddyAPI
)
new
Website
CaddyFile
(
domain
string
,
root
string
)
error
{
buffer
:=
bytes
.
NewBufferString
(
""
)
caddypath
:=
filepath
.
Join
(
c
.
RootDir
,
fmt
.
Sprintf
(
"%s.caddy"
,
domain
))
// build CaddyFileTemplate.
cfg
:=
NewWebsiteCaddyFile
{
DomainName
:
domain
,
DomainName
:
fmt
.
Sprintf
(
"https://%s"
,
domain
)
,
RootDir
:
root
,
}
tmpl
,
err
:=
template
.
New
(
"test"
)
.
Parse
(
staticWebsiteCaddyFileTempl
)
...
...
@@ -69,6 +69,52 @@ func (c *CaddyAPI) newCaddyFile(domain string, root string) error {
return
nil
}
func
(
c
*
CaddyAPI
)
newForwardCaddyFile
(
from
string
,
to
string
)
error
{
buffer
:=
bytes
.
NewBufferString
(
""
)
caddypath
:=
filepath
.
Join
(
c
.
RootDir
,
fmt
.
Sprintf
(
"%s.caddy"
,
from
))
// build CaddyFileTemplate.
cfg
:=
ForwardWebsiteCaddyFile
{
DomainName
:
fmt
.
Sprintf
(
"https://%s"
,
from
),
Target
:
to
,
}
tmpl
,
err
:=
template
.
New
(
"test"
)
.
Parse
(
forwardWebsiteCaddyFileTempl
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"failed to parse forward caddyfile template"
)
return
err
}
err
=
tmpl
.
Execute
(
buffer
,
cfg
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"failed to build forward caddyfile"
)
return
err
}
if
err
=
utils
.
CreateFile
(
caddypath
,
buffer
.
String
());
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"failed to create forward caddyfile"
)
return
err
}
return
nil
}
func
(
c
*
CaddyAPI
)
buildPayloadForForwardWebsite
(
domain
string
,
target
string
)
(
string
,
error
)
{
// build the payload for creating a new website.
buffer
:=
bytes
.
NewBufferString
(
""
)
cfg
:=
ForwardWebsitePayload
{
DomainName
:
domain
,
Target
:
target
,
}
tmpl
,
err
:=
template
.
New
(
"test"
)
.
Parse
(
forwardWebsitePayloadTempl
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"failed to parse forward payload template"
)
return
""
,
err
}
err
=
tmpl
.
Execute
(
buffer
,
cfg
)
if
err
!=
nil
{
log
.
WithError
(
err
)
.
Error
(
"failed to build forward payload"
)
return
""
,
err
}
return
buffer
.
String
(),
nil
}
func
(
c
*
CaddyAPI
)
CreateWebsite
(
domain
string
,
resource
string
)
error
{
// unzip resource to the website root directory.
websiteRoot
:=
filepath
.
Join
(
c
.
RootDir
,
"websites"
,
domain
)
...
...
@@ -76,7 +122,7 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
return
err
}
// create caddyfile.
if
err
:=
c
.
newCaddyFile
(
domain
,
websiteRoot
);
err
!=
nil
{
if
err
:=
c
.
new
Website
CaddyFile
(
domain
,
websiteRoot
);
err
!=
nil
{
return
err
}
...
...
@@ -97,7 +143,24 @@ func (c *CaddyAPI) CreateWebsite(domain string, resource string) error {
}
func
(
c
*
CaddyAPI
)
ForwardWebsite
(
param
types
.
ForwardWebsite
)
error
{
// todo: create a new site in Caddy and set the target to the domain.
// create caddyfile.
if
err
:=
c
.
newForwardCaddyFile
(
param
.
Domain
,
param
.
Target
);
err
!=
nil
{
return
err
}
// build api payload.
payload
,
err
:=
c
.
buildPayloadForForwardWebsite
(
param
.
Domain
,
param
.
Target
)
if
err
!=
nil
{
return
err
}
// send put request to the caddy api.
path
:=
fmt
.
Sprintf
(
"%s/config/apps/http/servers/srv0/routes/%d"
,
c
.
Url
,
0
)
log
.
WithField
(
"payload"
,
payload
)
.
Debug
(
"sending payload to caddy"
)
if
err
:=
c
.
put
(
path
,
[]
byte
(
payload
));
err
!=
nil
{
return
err
}
return
nil
}
...
...
caddy/template.go
View file @
5532c088
...
...
@@ -9,6 +9,63 @@ var staticWebsiteCaddyFileTempl = `
}
`
var
forwardWebsiteCaddyFileTempl
=
`
{{ .DomainName }} {
reverse_proxy https://{{ .Target }} {
header_up Host {upstream_hostport}
transport http {
tls_insecure_skip_verify
}
}
}
`
var
forwardWebsitePayloadTempl
=
`
{
"handle": [
{
"handler": "subroute",
"routes": [
{
"handle": [
{
"handler": "reverse_proxy",
"headers": {
"request": {
"set": {
"Host": [
"{http.reverse_proxy.upstream.hostport}"
]
}
}
},
"transport": {
"protocol": "http",
"tls": {
"insecure_skip_verify": true
}
},
"upstreams": [
{
"dial": "{{ .Target }}:443"
}
]
}
]
}
]
}
],
"match": [
{
"host": [
"{{ .DomainName }}"
]
}
],
"terminal": true
}`
var
newWebsitePayloadTempl
=
`
{
"handle": [
...
...
caddy/type.go
View file @
5532c088
...
...
@@ -10,3 +10,13 @@ type StaticWebsiteCreatePayload struct {
WebsiteCaddyFile
string
RootDir
string
}
type
ForwardWebsiteCaddyFile
struct
{
DomainName
string
Target
string
}
type
ForwardWebsitePayload
struct
{
DomainName
string
Target
string
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment