mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2024-12-25 19:55:38 +08:00
73 lines
1.3 KiB
Go
73 lines
1.3 KiB
Go
package resource
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
|
)
|
|
|
|
type FileVehicle struct {
|
|
path string
|
|
}
|
|
|
|
func (f *FileVehicle) Type() types.VehicleType {
|
|
return types.File
|
|
}
|
|
|
|
func (f *FileVehicle) Path() string {
|
|
return f.path
|
|
}
|
|
|
|
func (f *FileVehicle) Read() ([]byte, error) {
|
|
return os.ReadFile(f.path)
|
|
}
|
|
|
|
func NewFileVehicle(path string) *FileVehicle {
|
|
return &FileVehicle{path: path}
|
|
}
|
|
|
|
type HTTPVehicle struct {
|
|
url string
|
|
path string
|
|
}
|
|
|
|
func (h *HTTPVehicle) Url() string {
|
|
return h.url
|
|
}
|
|
|
|
func (h *HTTPVehicle) Type() types.VehicleType {
|
|
return types.HTTP
|
|
}
|
|
|
|
func (h *HTTPVehicle) Path() string {
|
|
return h.path
|
|
}
|
|
|
|
func (h *HTTPVehicle) Read() ([]byte, error) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*20)
|
|
defer cancel()
|
|
resp, err := clashHttp.HttpRequest(ctx, h.url, http.MethodGet, nil, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
|
return nil, errors.New(resp.Status)
|
|
}
|
|
buf, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return buf, nil
|
|
}
|
|
|
|
func NewHTTPVehicle(url string, path string) *HTTPVehicle {
|
|
return &HTTPVehicle{url, path}
|
|
}
|