Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CoreX-wg-easy
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
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
Hendrik Garske
CoreX-wg-easy
Commits
6dea7a86
Commit
6dea7a86
authored
Dec 27, 2025
by
Hendrik Garske
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix: Verbesserte Fehlerbehandlung fuer WG API Endpunkte
parent
0c63f7a4
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
12 deletions
+60
-12
route.ts
app/api/clients/route.ts
+26
-5
wg-api.ts
lib/wg-api.ts
+34
-7
No files found.
app/api/clients/route.ts
View file @
6dea7a86
...
@@ -27,12 +27,33 @@ export async function POST(request: NextRequest) {
...
@@ -27,12 +27,33 @@ export async function POST(request: NextRequest) {
)
)
}
}
try
{
const
client
=
await
wgApi
.
createClient
({
const
client
=
await
wgApi
.
createClient
({
name
:
name
.
trim
(),
name
:
name
.
trim
(),
allowedIPs
:
allowedIPs
||
undefined
,
allowedIPs
:
allowedIPs
||
undefined
,
})
})
return
NextResponse
.
json
(
client
,
{
status
:
201
})
return
NextResponse
.
json
(
client
,
{
status
:
201
})
}
catch
(
apiError
)
{
console
.
error
(
"WG API Error:"
,
apiError
)
const
errorMessage
=
apiError
instanceof
Error
?
apiError
.
message
:
"Failed to create client"
// Check if it's a 404 - API endpoint might be wrong
if
(
errorMessage
.
includes
(
'404'
)
||
errorMessage
.
includes
(
'not found'
))
{
return
NextResponse
.
json
(
{
error
:
"WireGuard API endpoint not found. Please check WG_API_URL configuration."
,
details
:
errorMessage
},
{
status
:
503
}
)
}
return
NextResponse
.
json
(
{
error
:
errorMessage
},
{
status
:
500
}
)
}
}
catch
(
error
)
{
}
catch
(
error
)
{
console
.
error
(
"Error creating client:"
,
error
)
console
.
error
(
"Error creating client:"
,
error
)
const
errorMessage
=
error
instanceof
Error
?
error
.
message
:
"Failed to create client"
const
errorMessage
=
error
instanceof
Error
?
error
.
message
:
"Failed to create client"
...
...
lib/wg-api.ts
View file @
6dea7a86
...
@@ -66,6 +66,8 @@ export class WgApiClient {
...
@@ -66,6 +66,8 @@ export class WgApiClient {
headers
[
'Authorization'
]
=
`Basic
${
Buffer
.
from
(
`:
${
this
.
password
}
`
).
toString
(
'base64'
)}
`
headers
[
'Authorization'
]
=
`Basic
${
Buffer
.
from
(
`:
${
this
.
password
}
`
).
toString
(
'base64'
)}
`
}
}
console
.
log
(
`[WG API] Requesting:
${
url
}
`
)
const
response
=
await
fetch
(
url
,
{
const
response
=
await
fetch
(
url
,
{
...
options
,
...
options
,
headers
,
headers
,
...
@@ -73,12 +75,22 @@ export class WgApiClient {
...
@@ -73,12 +75,22 @@ export class WgApiClient {
if
(
!
response
.
ok
)
{
if
(
!
response
.
ok
)
{
const
errorText
=
await
response
.
text
()
const
errorText
=
await
response
.
text
()
throw
new
Error
(
`WG API Error:
${
response
.
status
}
${
errorText
}
`
)
// Check if it's HTML (404 page) - means endpoint doesn't exist
if
(
errorText
.
includes
(
'<!DOCTYPE html>'
)
||
errorText
.
includes
(
'<html'
))
{
throw
new
Error
(
`WG API Endpoint not found:
${
endpoint
}
(404). Check if WG_API_URL is correct:
${
this
.
baseUrl
}
`
)
}
throw
new
Error
(
`WG API Error:
${
response
.
status
}
${
errorText
.
substring
(
0
,
200
)}
`
)
}
}
const
contentType
=
response
.
headers
.
get
(
'content-type'
)
if
(
contentType
&&
contentType
.
includes
(
'application/json'
))
{
return
response
.
json
()
return
response
.
json
()
}
}
// If not JSON, return as text
return
response
.
text
()
as
unknown
as
T
}
async
getStats
():
Promise
<
WgStats
>
{
async
getStats
():
Promise
<
WgStats
>
{
try
{
try
{
return
await
this
.
request
<
WgStats
>
(
'/api/sessions'
)
return
await
this
.
request
<
WgStats
>
(
'/api/sessions'
)
...
@@ -111,7 +123,8 @@ export class WgApiClient {
...
@@ -111,7 +123,8 @@ export class WgApiClient {
}
}
async
createClient
(
data
:
CreateClientRequest
):
Promise
<
CreateClientResponse
>
{
async
createClient
(
data
:
CreateClientRequest
):
Promise
<
CreateClientResponse
>
{
return
this
.
request
<
CreateClientResponse
>
(
'/api/users'
,
{
// wg-easy uses /api/users endpoint
return
await
this
.
request
<
CreateClientResponse
>
(
'/api/users'
,
{
method
:
'POST'
,
method
:
'POST'
,
body
:
JSON
.
stringify
(
data
),
body
:
JSON
.
stringify
(
data
),
})
})
...
@@ -141,12 +154,19 @@ export class WgApiClient {
...
@@ -141,12 +154,19 @@ export class WgApiClient {
headers
[
'Authorization'
]
=
`Basic
${
Buffer
.
from
(
`:
${
this
.
password
}
`
).
toString
(
'base64'
)}
`
headers
[
'Authorization'
]
=
`Basic
${
Buffer
.
from
(
`:
${
this
.
password
}
`
).
toString
(
'base64'
)}
`
}
}
const
response
=
await
fetch
(
`
${
this
.
baseUrl
}
/api/users/
${
id
}
/configuration`
,
{
const
url
=
`
${
this
.
baseUrl
}
/api/users/
${
id
}
/configuration`
console
.
log
(
`[WG API] Fetching config from:
${
url
}
`
)
const
response
=
await
fetch
(
url
,
{
headers
,
headers
,
})
})
if
(
!
response
.
ok
)
{
if
(
!
response
.
ok
)
{
throw
new
Error
(
`Failed to get config:
${
response
.
status
}
`
)
const
errorText
=
await
response
.
text
()
if
(
errorText
.
includes
(
'<!DOCTYPE html>'
)
||
errorText
.
includes
(
'<html'
))
{
throw
new
Error
(
`Config endpoint not found (404). Check if WG_API_URL is correct:
${
this
.
baseUrl
}
`
)
}
throw
new
Error
(
`Failed to get config:
${
response
.
status
}
${
errorText
.
substring
(
0
,
200
)}
`
)
}
}
return
response
.
text
()
return
response
.
text
()
...
@@ -158,12 +178,19 @@ export class WgApiClient {
...
@@ -158,12 +178,19 @@ export class WgApiClient {
headers
[
'Authorization'
]
=
`Basic
${
Buffer
.
from
(
`:
${
this
.
password
}
`
).
toString
(
'base64'
)}
`
headers
[
'Authorization'
]
=
`Basic
${
Buffer
.
from
(
`:
${
this
.
password
}
`
).
toString
(
'base64'
)}
`
}
}
const
response
=
await
fetch
(
`
${
this
.
baseUrl
}
/api/users/
${
id
}
/qr-code`
,
{
const
url
=
`
${
this
.
baseUrl
}
/api/users/
${
id
}
/qr-code`
console
.
log
(
`[WG API] Fetching QR code from:
${
url
}
`
)
const
response
=
await
fetch
(
url
,
{
headers
,
headers
,
})
})
if
(
!
response
.
ok
)
{
if
(
!
response
.
ok
)
{
throw
new
Error
(
`Failed to get QR code:
${
response
.
status
}
`
)
const
errorText
=
await
response
.
text
()
if
(
errorText
.
includes
(
'<!DOCTYPE html>'
)
||
errorText
.
includes
(
'<html'
))
{
throw
new
Error
(
`QR code endpoint not found (404). Check if WG_API_URL is correct:
${
this
.
baseUrl
}
`
)
}
throw
new
Error
(
`Failed to get QR code:
${
response
.
status
}
${
errorText
.
substring
(
0
,
200
)}
`
)
}
}
return
response
.
text
()
return
response
.
text
()
...
...
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