2022-07-11 21:30:34 +08:00
|
|
|
package resource
|
2019-12-08 12:17:24 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-11-09 08:06:37 +08:00
|
|
|
clashHttp "github.com/Dreamacro/clash/component/http"
|
2022-06-04 19:14:39 +08:00
|
|
|
types "github.com/Dreamacro/clash/constant/provider"
|
2021-10-09 20:35:06 +08:00
|
|
|
"io"
|
2019-12-08 12:17:24 +08:00
|
|
|
"net/http"
|
2021-10-09 20:35:06 +08:00
|
|
|
"os"
|
2019-12-08 12:17:24 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type FileVehicle struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
func (f *FileVehicle) Type() types.VehicleType {
|
|
|
|
return types.File
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileVehicle) Path() string {
|
|
|
|
return f.path
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *FileVehicle) Read() ([]byte, error) {
|
2021-10-09 20:35:06 +08:00
|
|
|
return os.ReadFile(f.path)
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileVehicle(path string) *FileVehicle {
|
|
|
|
return &FileVehicle{path: path}
|
|
|
|
}
|
|
|
|
|
|
|
|
type HTTPVehicle struct {
|
|
|
|
url string
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
2022-11-05 02:24:08 +08:00
|
|
|
func (h *HTTPVehicle) Url() string {
|
|
|
|
return h.url
|
|
|
|
}
|
|
|
|
|
2021-07-04 20:32:59 +08:00
|
|
|
func (h *HTTPVehicle) Type() types.VehicleType {
|
|
|
|
return types.HTTP
|
2019-12-08 12:17:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2022-11-09 08:06:37 +08:00
|
|
|
resp, err := clashHttp.HttpRequest(ctx, h.url, http.MethodGet, nil, nil)
|
2020-04-20 21:22:23 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-29 11:28:22 +08:00
|
|
|
defer resp.Body.Close()
|
2021-10-09 20:35:06 +08:00
|
|
|
buf, err := io.ReadAll(resp.Body)
|
2019-12-08 12:17:24 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewHTTPVehicle(url string, path string) *HTTPVehicle {
|
|
|
|
return &HTTPVehicle{url, path}
|
|
|
|
}
|