Skip to content

Commit

Permalink
Add Client address to request data .
Browse files Browse the repository at this point in the history
  • Loading branch information
AndroThink committed Nov 24, 2019
1 parent 3e13981 commit c769881
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 20 deletions.
25 changes: 23 additions & 2 deletions server/src/main/java/com/androthink/server/core/RoutesHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ private RoutesHandler() {
this.routeList = new ArrayList<>();
}

void Handle(Request request, ResponseHandler responseHandler) throws IOException {
void Handle(@NonNull Request request, ResponseHandler responseHandler) throws IOException {

if (!isAllowedMethod(request.getMethod()))
responseHandler.sendJsonResponse(ServerHelper.RESPONSE_CODE.METHOD_NOT_ALLOWED,
Expand Down Expand Up @@ -79,7 +79,28 @@ void Handle(Request request, ResponseHandler responseHandler) throws IOException

Route startedWithRoute = getRouteWithStartWith(request.getMethod(), request.getRoutePath());
if (startedWithRoute != null) {
startedWithRoute.getCallBack().onRequested(request, responseHandler);
if (startedWithRoute.isAuth()) {
if (isApiRoute(request.getRoutePath())) {
String authKey = getAuthorizationKey(request.getHeaders());
if (authKey != null) {
request.setApiKey(authKey);
startedWithRoute.getCallBack().onRequested(request, responseHandler);
return;
}

responseHandler.sendJsonResponse(ServerHelper.RESPONSE_CODE.UNAUTHORIZED,
"{\"status\":false,\"error\":\"Error 401 UnAuthorized !\"}");
} else {
String authKey = getAuthorizationToken(request.getRequestCookies());
if (authKey != null) {
request.setApiKey(authKey);
startedWithRoute.getCallBack().onRequested(request, responseHandler);
} else
responseHandler.sendAssetFile(ServerHelper.RESPONSE_CODE.UNAUTHORIZED,ServerHelper.CONTENT_TYPE.HTML, "html/login.html");
}
} else
startedWithRoute.getCallBack().onRequested(request, responseHandler);

return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import android.content.Context;
import android.content.res.Resources;

import androidx.annotation.NonNull;

import com.androthink.server.callback.RequestCallBack;
import com.androthink.server.handler.ResponseHandler;
import com.androthink.server.helper.NetworkHelper;
import com.androthink.server.helper.ServerHelper;
import com.androthink.server.model.Request;

Expand All @@ -25,10 +28,10 @@ class ServerRequestThread extends Thread {

private Request request;

ServerRequestThread(Context context, Socket socket, String requestId, RequestCallBack requestCallBack) {
ServerRequestThread(Context context, @NonNull Socket socket, String requestId, RequestCallBack requestCallBack) {

this.context = context;
this.request = new Request(requestId, requestCallBack);
this.request = new Request(requestId, NetworkHelper.getIp(socket.getInetAddress(),true), requestCallBack);
this.clientSocket = socket;

start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,27 @@ public static String getIPAddress(boolean useIPv4) throws SocketException{
List<InetAddress> addressList = Collections.list(netInterface.getInetAddresses());
for (InetAddress address : addressList) {
if (!address.isLoopbackAddress()) {
String addressString = address.getHostAddress();
boolean isIPv4 = addressString.indexOf(':') < 0;
if (useIPv4) {
if (isIPv4)
return addressString;
} else {
if (!isIPv4) {
int delim = addressString.indexOf('%'); // drop ip6 zone suffix
return delim < 0 ? addressString.toUpperCase() : addressString.substring(0, delim).toUpperCase();
}
}
return getIp(address,useIPv4);
}
}
}
return "";
}

public static String getIp(@NonNull InetAddress address,boolean useIPv4){
String addressString = address.getHostAddress();

boolean isIPv4 = addressString.indexOf(':') < 0;
if (useIPv4) {
if (isIPv4)
return addressString;
} else {
if (!isIPv4) {
int delim = addressString.indexOf('%'); // drop ip6 zone suffix
return delim < 0 ? addressString.toUpperCase() : addressString.substring(0, delim).toUpperCase();
}
}

return "";
}
}
11 changes: 6 additions & 5 deletions server/src/main/java/com/androthink/server/model/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ public class Request {
private String requestProtocol;
private String requestRoutePath;

private String requestClientAddress;

private final RequestCallBack requestCallBack;

private Object requestPayload;
private Map<String,String> requestHeaders;
private Map<String,String> requestCookies;

public Request(String requestId,RequestCallBack callBack){
public Request(String requestId,String clientAddress,RequestCallBack callBack){
this.requestId = requestId;
this.requestClientAddress = clientAddress;
this.requestCallBack = callBack;
this.requestHeaders = new HashMap<>();
this.requestCookies = new HashMap<>();
Expand All @@ -38,10 +41,6 @@ public String getRequestId() {
return requestId;
}

public void setRequestId(String requestId) {
this.requestId = requestId;
}

public String getMethod() {
return requestMethod;
}
Expand All @@ -66,6 +65,8 @@ public void setRoutePath(String requestRoutePath) {
this.requestRoutePath = requestRoutePath;
}

public String getClientAddress() { return requestClientAddress; }

public Object getPayload() {
return requestPayload;
}
Expand Down

0 comments on commit c769881

Please sign in to comment.