mirror of https://github.com/FongMi/TV.git
parent
3d4140d6d9
commit
8c69744222
@ -0,0 +1,58 @@ |
||||
package com.fongmi.bear.server; |
||||
|
||||
import com.fongmi.bear.ApiConfig; |
||||
|
||||
import java.io.InputStream; |
||||
import java.util.Map; |
||||
|
||||
import fi.iki.elonen.NanoHTTPD; |
||||
|
||||
public class Nano extends NanoHTTPD { |
||||
|
||||
private Listener mListener; |
||||
|
||||
public Nano() { |
||||
super(9978); |
||||
} |
||||
|
||||
@Override |
||||
public Response serve(IHTTPSession session) { |
||||
if (session.getUri().isEmpty()) return super.serve(session); |
||||
String url = session.getUri().trim(); |
||||
if (url.indexOf('?') >= 0) url = url.substring(0, url.indexOf('?')); |
||||
if (session.getMethod() == Method.GET) { |
||||
if (url.equals("/proxy")) { |
||||
Map<String, String> params = session.getParms(); |
||||
if (params.containsKey("do")) { |
||||
Object[] rs = ApiConfig.get().proxyLocal(params); |
||||
try { |
||||
int code = (int) rs[0]; |
||||
String mime = (String) rs[1]; |
||||
InputStream stream = rs[2] != null ? (InputStream) rs[2] : null; |
||||
return NanoHTTPD.newChunkedResponse(Response.Status.lookup(code), mime, stream); |
||||
} catch (Exception e) { |
||||
return NanoHTTPD.newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR, NanoHTTPD.MIME_PLAINTEXT, "500"); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return super.serve(session); |
||||
} |
||||
|
||||
public Listener getListener() { |
||||
return mListener; |
||||
} |
||||
|
||||
public void setListener(Listener listener) { |
||||
this.mListener = listener; |
||||
} |
||||
|
||||
public interface Listener { |
||||
|
||||
void onTextReceived(String text); |
||||
|
||||
void onApiReceived(String url); |
||||
|
||||
void onPushReceived(String url); |
||||
} |
||||
} |
||||
@ -1,12 +1,54 @@ |
||||
package com.fongmi.bear.server; |
||||
|
||||
import fi.iki.elonen.NanoHTTPD; |
||||
import com.fongmi.bear.utils.Utils; |
||||
|
||||
public class Server extends NanoHTTPD { |
||||
public class Server implements Nano.Listener { |
||||
|
||||
public static int port = 9978; |
||||
private Nano mNano; |
||||
|
||||
private static class Loader { |
||||
static volatile Server INSTANCE = new Server(); |
||||
} |
||||
|
||||
public static Server get() { |
||||
return Loader.INSTANCE; |
||||
} |
||||
|
||||
public String getAddress(boolean local) { |
||||
return "http://" + (local ? "127.0.0.1" : Utils.getIP()) + ":" + mNano.getListeningPort() + "/"; |
||||
} |
||||
|
||||
public void start() { |
||||
if (mNano != null) return; |
||||
try { |
||||
mNano = new Nano(); |
||||
mNano.setListener(this); |
||||
mNano.start(); |
||||
} catch (Exception e) { |
||||
mNano.stop(); |
||||
mNano = null; |
||||
} |
||||
} |
||||
|
||||
public void stop() { |
||||
if (mNano != null) { |
||||
mNano.stop(); |
||||
mNano = null; |
||||
} |
||||
} |
||||
|
||||
@Override |
||||
public void onTextReceived(String text) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void onApiReceived(String url) { |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void onPushReceived(String url) { |
||||
|
||||
public Server() { |
||||
super(port); |
||||
} |
||||
} |
||||
|
||||
Loading…
Reference in new issue